git-svn-id: https://wala.svn.sourceforge.net/svnroot/wala/trunk@630 f5eafffb-2e1d-0410-98e4-8ec43c5233c4

This commit is contained in:
dolby-oss 2007-02-02 17:38:49 +00:00
parent 5327966ce3
commit 862ad01846
35 changed files with 5508 additions and 0 deletions

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="data"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@ -0,0 +1,6 @@
#*
*#
*~
*.class
bin
build

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>com.ibm.wala.automaton.test</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,13 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Test Plug-in for Automaton
Bundle-SymbolicName: com.ibm.capa.util.automaton.test
Bundle-Version: 1.0.0
Bundle-Activator: com.ibm.wala.automaton.test.TestPlugin
Bundle-Vendor: IBM
Bundle-Localization: plugin
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime,
com.ibm.capa.util.automaton,
org.junit
Eclipse-AutoStart: true

View File

@ -0,0 +1,4 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.

View File

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
=======================================================================
CAPA Util build file
=======================================================================
-->
<project name="com.ibm.capa.util.automaton.test" default="jar" basedir=".">
<!-- Software version details -->
<property name="name" value="capautilautomatontest" />
<property name="module_name" value="com.ibm.capa.util.automaton.test" />
<property name="Name" value="CAPA Util Automaton Test" />
<property name="version" value="1-alpha" />
<!-- Compilation switches -->
<property name="debug" value="true" />
<property name="deprecation" value="false" />
<property name="optimize" value="true" />
<property name="javacFailOnError" value="true" />
<property name="javacDebugInfo" value="on" />
<property name="javacVerbose" value="false" />
<!-- Set global properties for this build -->
<property name="src" value="src" />
<property name="src.tests" value="src" />
<property name="build" value="build" />
<property name="build.result" value="${basedir}" />
<property name="build.tests" value="${build}" />
<property name="build.javadocs" value="${build}/javadocs" />
<property name="publish.javadocs" value="javadocs/com.ibm.capa.util.automaton.test" />
<property name="dist" value="dist" />
<property name="etc" value="etc" />
<property name="docs" value="docs" />
<property name="lib" value="lib" />
<property name="mainlib" value="../mainlib" />
<property name="sharedlib" value="../sharedlib" />
<property name="templates" value="../com.ibm.safe/templates"/>
<property name="specifications" value="../com.ibm.safe/specifications"/>
<property name="cvsroot" value="/gsa/watgsa/home/d/o/dolby/cvs/CAstAnalysis"/>
<import file="${sharedlib}/scripts/common-tests-targets.xml"/>
</project>

View File

@ -0,0 +1,19 @@
a1 = Automaton(){
init = <q1>;
final = {<qq>};
transitions = {
<q1>(a) -> <q2>;
<q2>(b) -> <q3>;
<q3>(c) -> <q4>;
}
};
a1 = Automaton(){
init = <q1>;
final = {<qq>};
transitions = {
<q1>(a) -> <q2>;
<q2>(b) -> <q3>;
<q3>(c) -> <q4>;
}
};

View File

@ -0,0 +1,51 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test;
import com.ibm.wala.automaton.string.State;
import com.ibm.wala.automaton.string.Symbol;
import com.ibm.wala.automaton.string.Variable;
import junit.framework.TestCase;
abstract public class AutomatonJunitBase extends TestCase {
protected State
s0 = new State("s0"),
s1 = new State("s1"),
s2 = new State("s2"),
s3 = new State("s3"),
s4 = new State("s4"),
s5 = new State("s5"),
s6 = new State("s6"),
s7 = new State("s7"),
s8 = new State("s8");
protected Symbol
a = new Symbol("a"),
b = new Symbol("b"),
c = new Symbol("c"),
A = new Symbol("A"),
B = new Symbol("B"),
C = new Symbol("C"),
i0 = new Symbol("i0"),
i1 = new Symbol("i1"),
i2 = new Symbol("i2"),
i3 = new Symbol("i3"),
i4 = new Symbol("i4"),
i5 = new Symbol("i5"),
i6 = new Symbol("i6");
protected Variable
v0 = new Variable("v0"),
v1 = new Variable("v1"),
v2 = new Variable("v2"),
v3 = new Variable("v3"),
v4 = new Variable("v4"),
v5 = new Variable("v5");
}

View File

@ -0,0 +1,155 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.ibm.wala.automaton.AUtil;
import junit.framework.TestCase;
public class TestAUtils extends TestCase {
public void testList() {
List l1 = AUtil.list(new Object[]{"a", "b", "c"});
List l2 = new ArrayList();
l2.add("a");
l2.add("b");
l2.add("c");
assertEquals(l2, l1);
}
public void testSet() {
Set s1 = AUtil.set(new Object[]{"a", "b", "c"});
Set s2 = new HashSet();
s2.add("a");
s2.add("b");
s2.add("c");
assertEquals(s2, s1);
}
public void testAllList() {
Set s1 = AUtil.set(new Object[]{"a", "b", "c"});
Set s2 = AUtil.set(new Object[]{
AUtil.list(new Object[]{"a", "b", "c"}),
AUtil.list(new Object[]{"a", "c", "b"}),
AUtil.list(new Object[]{"b", "a", "c"}),
AUtil.list(new Object[]{"b", "c", "a"}),
AUtil.list(new Object[]{"c", "a", "b"}),
AUtil.list(new Object[]{"c", "b", "a"}),
});
Set comb = AUtil.allOrder(s1);
assertEquals(s2, comb);
}
public void testMap() {
Map m = AUtil.map(
new String[]{"a", "b", "c"},
new String[]{"A", "B", "C"});
Map expected = new HashMap();
expected.put("a", "A");
expected.put("b", "B");
expected.put("c", "C");
assertEquals(expected, m);
}
public void testMap2() {
Map m = AUtil.map(
new String[]{"a", "b", "c"},
new String[]{"A", "B"});
Map expected = new HashMap();
expected.put("a", "A");
expected.put("b", "B");
expected.put("c", null);
assertEquals(expected, m);
}
public void testMap3() {
Map m = AUtil.map(
new String[]{"a", "b"},
new String[]{"A", "B", "C"});
Map expected = new HashMap();
expected.put("a", "A");
expected.put("b", "B");
assertEquals(expected, m);
}
public void testUniqueName() {
Set names = AUtil.set(new String[]{"a1", "a2", "b1", "c"});
assertEquals("a3", AUtil.createUniqueName("a", names));
assertEquals("a4", AUtil.createUniqueName("a", names));
assertEquals("b2", AUtil.createUniqueName("b", names));
assertEquals("b3", AUtil.createUniqueName("b", names));
assertEquals("c1", AUtil.createUniqueName("c", names));
assertEquals("c2", AUtil.createUniqueName("c", names));
assertEquals("d1", AUtil.createUniqueName("d", names));
assertEquals("d2", AUtil.createUniqueName("d", names));
assertEquals("a10", AUtil.createUniqueName("a", names, 10));
assertEquals("a11", AUtil.createUniqueName("a", names, 10));
}
public void testMapper() {
List l1 = AUtil.list(new String[]{"a", "b", "c"});
List l2 = AUtil.list(new String[]{"a1", "b1", "c1"});
assertEquals(l2, AUtil.collect(l1, new AUtil.IElementMapper(){
public Object map(Object obj) {
String s = (String) obj;
return s + "1";
}
}));
}
public void testSelector() {
List l1 = AUtil.list(new String[]{"ab", "b", "cd", "e"});
List l2 = AUtil.list(new String[]{"ab", "cd"});
assertEquals(l2, AUtil.select(l1, new AUtil.IElementSelector(){
public boolean selected(Object obj) {
String s = (String) obj;
return s.length() > 1;
}
}));
}
/*
public void testPrettyFormat() {
String str1 = "{a:1, b:2, c:3}";
String str2 =
"{" + AUtils.lineSeparator +
" a:1," + AUtils.lineSeparator +
" b:2," + AUtils.lineSeparator +
" c:3" + AUtils.lineSeparator +
"}";
String formatted = AUtils.prettyFormat(str1);
assertEquals(str2, formatted);
}
public void testPrettyFormat2() {
String str1 = "{a:1, b:[0,1,2], c:3}";
String str2 =
"{" + AUtils.lineSeparator +
" a:1," + AUtils.lineSeparator +
" b:[" + AUtils.lineSeparator +
" 0," + AUtils.lineSeparator +
" 1," + AUtils.lineSeparator +
" 2" + AUtils.lineSeparator +
" ]," + AUtils.lineSeparator +
" c:3" + AUtils.lineSeparator +
"}";
String formatted = AUtils.prettyFormat(str1);
assertEquals(str2, formatted);
}
*/
}

View File

@ -0,0 +1,64 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test;
import org.eclipse.ui.plugin.*;
import org.eclipse.jface.resource.ImageDescriptor;
import org.osgi.framework.BundleContext;
/**
* The main plugin class to be used in the desktop.
*/
public class TestPlugin extends AbstractUIPlugin {
//The shared instance.
private static TestPlugin plugin;
/**
* The constructor.
*/
public TestPlugin() {
plugin = this;
}
/**
* This method is called upon plug-in activation
*/
public void start(BundleContext context) throws Exception {
super.start(context);
}
/**
* This method is called when the plug-in is stopped
*/
public void stop(BundleContext context) throws Exception {
super.stop(context);
plugin = null;
}
/**
* Returns the shared instance.
*/
public static TestPlugin getDefault() {
return plugin;
}
/**
* Returns an image descriptor for the image file at the given
* plug-in relative path.
*
* @param path the path
* @return the image descriptor
*/
public static ImageDescriptor getImageDescriptor(String path) {
return AbstractUIPlugin.imageDescriptorFromPlugin("com.ibm.capa.util.automaton.test", path);
}
}

View File

@ -0,0 +1,122 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.grammar.string;
import java.util.*;
import com.ibm.wala.automaton.grammar.string.*;
import com.ibm.wala.automaton.string.*;
import junit.framework.TestCase;
public class TestCFG extends TestCase {
public void testEquality(){
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("+"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("-"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("("), new Variable("A"), new Symbol(")")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("1")}),
new ProductionRule(new Variable("B"), new ISymbol[]{}),
}
);
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("("), new Variable("A"), new Symbol(")")}),
new ProductionRule(new Variable("B"), new ISymbol[]{}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("-"), new Variable("A")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("1")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("+"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0")}),
}
);
assertTrue(cfg1.equals(cfg2));
assertTrue(cfg2.equals(cfg1));
}
public void testEquality2(){
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("+"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("-"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("("), new Variable("A"), new Symbol(")")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("1")}),
new ProductionRule(new Variable("B"), new ISymbol[]{}),
}
);
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("("), new Variable("A"), new Symbol(")")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("1")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("+"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("-"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B")}),
}
);
assertFalse(cfg1.equals(cfg2));
assertFalse(cfg2.equals(cfg1));
}
public void testCFGCopy1() {
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("+"), new Variable("A")}),
});
ContextFreeGrammar cfg2 = (ContextFreeGrammar) cfg1.copy(SimpleGrammarCopier.defaultCopier);
assertEquals(cfg1, cfg2);
assertTrue(System.identityHashCode(cfg1.getStartSymbol())
== System.identityHashCode(cfg2.getStartSymbol()));
assertTrue(System.identityHashCode((new ArrayList(cfg1.getRules())).get(0))
== System.identityHashCode((new ArrayList(cfg2.getRules())).get(0)));
}
public void testCFGCopy2() {
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("+"), new Variable("A")}),
});
ContextFreeGrammar cfg2 = (ContextFreeGrammar) cfg1.copy(new DeepGrammarCopier(new SimpleRuleCopier()));
assertEquals(cfg1, cfg2);
assertTrue(System.identityHashCode(cfg1.getStartSymbol())
== System.identityHashCode(cfg2.getStartSymbol()));
assertFalse(System.identityHashCode((new ArrayList(cfg1.getRules())).get(0))
== System.identityHashCode((new ArrayList(cfg2.getRules())).get(0)));
}
public void testCFGCopy3() {
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("+"), new Variable("A")}),
});
ContextFreeGrammar cfg2 = (ContextFreeGrammar) cfg1.copy(new DeepGrammarCopier(new DeepRuleCopier(new SimpleSymbolCopier())));
assertEquals(cfg1, cfg2);
assertFalse(System.identityHashCode(cfg1.getStartSymbol())
== System.identityHashCode(cfg2.getStartSymbol()));
assertFalse(System.identityHashCode((new ArrayList(cfg1.getRules())).get(0))
== System.identityHashCode((new ArrayList(cfg2.getRules())).get(0)));
}
}

View File

@ -0,0 +1,495 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.grammar.string;
import java.util.Set;
import com.ibm.wala.automaton.AUtil;
import com.ibm.wala.automaton.grammar.string.*;
import com.ibm.wala.automaton.string.*;
import junit.framework.TestCase;
public class TestCFLReachability extends TestCase {
public void testReachabilitySimple(){
Automaton fst = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("2")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
}
);
ContextFreeGrammar cfg = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b")}),
}
);
Automaton fst2 = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("2")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
new CFLReachability.AnalysisTransition(new State("s2"), new State("s3"), new Variable("B"), new Symbol[]{new Symbol("2")}),
new CFLReachability.AnalysisTransition(new State("s1"), new State("s3"), new Variable("A"), new Symbol[]{new Symbol("1"), new Variable("B")}),
}
);
IAutomaton result = CFLReachability.analyze(fst, cfg, new CFLReachability.SimpleTransitionFactory());
assertEquals(fst2, result);
assertTrue(CFLReachability.isReachable(fst,cfg));
}
public void testReachabilitySimple2(){
Automaton fst = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("2")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
}
);
ContextFreeGrammar cfg = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b")}),
}
);
Automaton fst2 = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("2")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
new CFLReachability.AnalysisTransition(new State("s2"), new State("s3"), new Variable("B"), new Symbol[]{new Symbol("2")}),
new CFLReachability.AnalysisTransition(new State("s1"), new State("s3"), new Variable("A"), new Symbol[]{new Symbol("1"), new Variable("B")}),
new CFLReachability.AnalysisTransition(new State("s2"), new State("s3"), new Variable("B"), new Symbol[]{new Symbol("0"), new Variable("B")}),
}
);
assertEquals(fst2, CFLReachability.analyze(fst, cfg, new CFLReachability.SimpleTransitionFactory()));
}
public void testReachabilityWithAnalysisTransition(){
Automaton fst = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("2")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
}
);
ContextFreeGrammar cfg = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b")}),
}
);
Automaton fst2 = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("2")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
new CFLReachability.AnalysisTransition(
new State("s2"), new State("s3"), new Variable("B"), new Symbol[]{new Symbol("2")}),
new CFLReachability.AnalysisTransition(
new State("s1"), new State("s3"), new Variable("A"), new Symbol[]{new Symbol("1"), new Variable("B")}),
}
);
IAutomaton result = CFLReachability.analyze(fst, cfg, new CFLReachability.SimpleTransitionFactory());
assertEquals(fst2, result);
assertTrue(CFLReachability.isReachable(fst,cfg));
}
public void testReachabilityWithProductionRule(){
Automaton fst = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("2")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
}
);
ContextFreeGrammar cfg = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b")}),
}
);
Automaton fst2 = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("2")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
new CFLReachability.ProductionRuleTransition(
new State("s2"), new State("s3"), new Variable("B"), new Symbol[]{new Symbol("2")},
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b")})),
new CFLReachability.ProductionRuleTransition(
new State("s1"), new State("s3"), new Variable("A"), new Symbol[]{new Symbol("1"), new Symbol("2")},
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a"), new Variable("B")})),
}
);
IAutomaton result = CFLReachability.analyze(fst, cfg, new CFLReachability.ProductionRuleTransitionFactory());
assertEquals(fst2, result);
assertTrue(CFLReachability.isReachable(fst,cfg));
}
public void testReachabilityWithVariable(){
FilteredTransition.ICondition cond = new FilteredTransition.ICondition(){
public boolean accept(ISymbol symbol, IMatchContext ctx) {
if (symbol instanceof IVariable) {
return false;
}
if (symbol.getName().charAt(0) == (char)'a') {
return true;
}
return false;
}
};
Automaton fst = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new FilteredTransition(new State("s1"), new State("s2"), new Variable("A"), new ISymbol[]{}, null, cond),
new Transition(new State("s2"), new State("s3"), new Symbol("b")),
}
);
ContextFreeGrammar cfg = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("b")}),
}
);
Automaton fst2 = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new FilteredTransition(new State("s1"), new State("s2"), new Variable("A"), new ISymbol[]{}, null, cond),
new Transition(new State("s2"), new State("s3"), new Symbol("b")),
new CFLReachability.AnalysisTransition(new State("s2"), new State("s3"), new Variable("A"), new ISymbol[]{}),
}
);
IAutomaton result = CFLReachability.analyze(fst, cfg, new CFLReachability.SimpleTransitionFactory());
assertEquals(fst2, result);
}
public void testReachabilityWithFilteredTransition1() {
Automaton fst = new Automaton(
new State("s1"),
new State[]{new State("s1")},
new Transition[]{
new FilteredTransition(new State("s1"), new State("s1"), new Variable("x"), new FilteredTransition.ICondition(){
public boolean accept(ISymbol symbol, IMatchContext ctx) {
if (symbol.getName().equals("z")) {
return false;
}
else {
return true;
}
}
}),
});
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new ProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a"), new Symbol("a")}),
});
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new ProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a"), new Symbol("z"), new Symbol("a")}),
});
assertTrue(CFLReachability.containsAll(fst, cfg1));
assertFalse(CFLReachability.containsAll(fst, cfg2));
assertTrue(CFLReachability.containsSome(cfg1, fst));
assertFalse(CFLReachability.containsSome(cfg2, fst));
}
public void testReachability2(){
Automaton fst = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("2")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
}
);
ContextFreeGrammar cfg = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b")}),
}
);
Automaton fst2 = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("2")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
new CFLReachability.ProductionRuleTransition(
new State("s1"), new State("s3"), new Variable("A"), new Symbol[]{new Symbol("1"), new Symbol("2")},
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a"), new Variable("B")})),
new CFLReachability.ProductionRuleTransition(
new State("s1"), new State("s3"), new Variable("A"), new Symbol[]{new Symbol("1"), new Symbol("0"), new Variable("B")},
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a"), new Variable("B")})),
new CFLReachability.ProductionRuleTransition(
new State("s1"), new State("s3"), new Variable("A"), new Symbol[]{new Symbol("1"), new Symbol("0"), new Symbol("2")},
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a"), new Variable("B")})),
new CFLReachability.ProductionRuleTransition(
new State("s2"), new State("s3"), new Variable("B"), new Symbol[]{new Symbol("2")},
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b")})),
new CFLReachability.ProductionRuleTransition(
new State("s2"), new State("s3"), new Variable("B"), new Symbol[]{new Symbol("0"), new Symbol("2")},
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0"), new Variable("B")})),
new CFLReachability.ProductionRuleTransition(
new State("s2"), new State("s3"), new Variable("B"), new Symbol[]{new Symbol("0"), new Variable("B")},
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0"), new Variable("B")})),
}
);
IAutomaton result = CFLReachability.analyze(fst, cfg, new CFLReachability.ProductionRuleTransitionFactory());
assertEquals(fst2, result);
}
public void testReachabilityRecursion(){
Automaton fst = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("2")}),
new Transition(new State("s3"), new State("s1"), new Symbol("c"), new Symbol[]{new Symbol("3")}),
new Transition(new State("s3"), new State("s3"), new Symbol("c"), new Symbol[]{new Symbol("3")}),
}
);
ContextFreeGrammar cfg = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b"), new Variable("C")}),
new ProductionRule(new Variable("C"), new ISymbol[]{new Symbol("c"), new Variable("A")}),
new ProductionRule(new Variable("C"), new ISymbol[]{new Symbol("c")}),
}
);
Automaton fst2 = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("2")}),
new Transition(new State("s3"), new State("s1"), new Symbol("c"), new Symbol[]{new Symbol("3")}),
new Transition(new State("s3"), new State("s3"), new Symbol("c"), new Symbol[]{new Symbol("3")}),
new CFLReachability.AnalysisTransition(new State("s1"), new State("s1"), new Variable("A"), new Symbol[]{new Symbol("1"), new Variable("B")}),
new CFLReachability.AnalysisTransition(new State("s1"), new State("s3"), new Variable("A"), new Symbol[]{new Symbol("1"), new Variable("B")}),
new CFLReachability.AnalysisTransition(new State("s2"), new State("s1"), new Variable("B"), new Symbol[]{new Symbol("2"), new Variable("C")}),
new CFLReachability.AnalysisTransition(new State("s2"), new State("s3"), new Variable("B"), new Symbol[]{new Symbol("2"), new Variable("C")}),
new CFLReachability.AnalysisTransition(new State("s3"), new State("s1"), new Variable("C"), new Symbol[]{new Symbol("3")}),
new CFLReachability.AnalysisTransition(new State("s3"), new State("s1"), new Variable("C"), new Symbol[]{new Symbol("3"), new Variable("A")}),
new CFLReachability.AnalysisTransition(new State("s3"), new State("s3"), new Variable("C"), new Symbol[]{new Symbol("3")}),
new CFLReachability.AnalysisTransition(new State("s3"), new State("s3"), new Variable("C"), new Symbol[]{new Symbol("3"), new Variable("A")}),
}
);
assertEquals(fst2, CFLReachability.analyze(fst, cfg, new CFLReachability.SimpleTransitionFactory()));
}
public void testReachabilityTraceable(){
Automaton fst = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("2")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
}
);
ContextFreeGrammar cfg = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b")}),
}
);
Automaton fst2 = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("2")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
new CFLReachability.AnalysisTransition(new State("s1"), new State("s3"), new Variable("A"), new Symbol[]{new Symbol("1"), new Variable("B")}),
new CFLReachability.AnalysisTransition(new State("s2"), new State("s3"), new Variable("B"), new Symbol[]{new Symbol("2")}),
new CFLReachability.AnalysisTransition(new State("s2"), new State("s3"), new Variable("B"), new Symbol[]{new Symbol("0"), new Variable("B")}),
/*
new ProductionRuleTransition(
new State("s2"), new State("s3"), new Variable("B"), new Symbol[]{new Symbol("2")},
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b")})),
new ProductionRuleTransition(
new State("s1"), new State("s3"), new Variable("A"), new Symbol[]{new Symbol("1"), new Variable("B")},
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a"), new Variable("B")})),
new ProductionRuleTransition(
new State("s2"), new State("s3"), new Variable("B"), new Symbol[]{new Symbol("0"), new Variable("B")},
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0"), new Variable("B")})),
*/
}
);
Set expected = AUtil.set(new ITransition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("2")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
new CFLReachability.AnalysisTransition(new State("s1"), new State("s3"), new Variable("A"), new Symbol[]{new Symbol("1"), new Variable("B")}),
new CFLReachability.AnalysisTransition(new State("s2"), new State("s3"), new Variable("B"), new Symbol[]{new Symbol("2")}),
new CFLReachability.AnalysisTransition(new State("s2"), new State("s3"), new Variable("B"), new Symbol[]{new Symbol("0"), new Variable("B")}),
});
CFLReachability.ITransitionFactory factory = new CFLReachability.SimpleTransitionFactory();
CFLReachability.TraceableTransitionFactory traceableFactory = new CFLReachability.TraceableTransitionFactory(factory);
IAutomaton result = CFLReachability.analyze(fst, cfg, traceableFactory);
assertEquals(fst2, result);
Set s1 = CFLReachability.getCorrespondingTransitions(result, cfg);
Set s2 = traceableFactory.getCorrespondingTransitions();
Set c1 = CFLReachability.selectConnectedTransitions(
s1,
new State("s1"), new State[]{new State("s3")}, new Variable("A"));
Set c2 = CFLReachability.selectConnectedTransitions(
s2,
new State("s1"), new State[]{new State("s3")}, new Variable("A"));
assertTrue(c1.equals(c2));
assertEquals(expected, c1);
}
public void testGetCorrespondingTransitions(){
ContextFreeGrammar cfg = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b")}),
}
);
Automaton fst = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("2")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
new Transition(new State("s2"), new State("s3"), new Variable("B"), new Symbol[]{new Symbol("2")}),
new Transition(new State("s1"), new State("s3"), new Variable("A"), new Symbol[]{new Symbol("1"), new Variable("B")}),
}
);
Set expected = AUtil.set(new CFLReachability.CorrespondingPath[]{
new CFLReachability.CorrespondingPath(
new Transition(new State("s2"), new State("s3"), new Variable("B"), new Symbol[]{new Symbol("2")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("2")})
),
new CFLReachability.CorrespondingPath(
new Transition(new State("s1"), new State("s3"), new Variable("A"), new Symbol[]{new Symbol("1"), new Variable("B")}),
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s2"), new State("s3"), new Variable("B"), new Symbol[]{new Symbol("2")})
),
});
Set transitions = CFLReachability.getCorrespondingTransitions(fst, cfg);
assertEquals(expected, transitions);
}
public void testReachabilityWithEpsilonTransition(){
Automaton fst = new Automaton(
new State("s1"),
new State[]{new State("s4")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s2"), new State("s3"), Transition.EpsilonSymbol),
new Transition(new State("s3"), new State("s4"), new Symbol("b"), new Symbol[]{new Symbol("2")}),
new Transition(new State("s3"), new State("s3"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
}
);
ContextFreeGrammar cfg = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b")}),
}
);
Automaton fst2 = new Automaton(
new State("s1"),
new State[]{new State("s4")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s2"), new State("s4"), new Symbol("b"), new Symbol[]{new Symbol("2")}),
new Transition(new State("s3"), new State("s4"), new Symbol("b"), new Symbol[]{new Symbol("2")}),
new Transition(new State("s2"), new State("s3"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
new Transition(new State("s3"), new State("s3"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
new CFLReachability.AnalysisTransition(new State("s1"), new State("s4"), new Variable("A"), new Symbol[]{new Symbol("1"), new Variable("B")}),
new CFLReachability.AnalysisTransition(new State("s2"), new State("s4"), new Variable("B"), new Symbol[]{new Symbol("2")}),
new CFLReachability.AnalysisTransition(new State("s3"), new State("s4"), new Variable("B"), new Symbol[]{new Symbol("2")}),
}
);
IAutomaton result = CFLReachability.analyze(fst, cfg, new CFLReachability.SimpleTransitionFactory());
assertEquals(fst2, result);
assertTrue(CFLReachability.isReachable(fst,cfg));
//assertTrue(CFLReachability.isReachable(fst,cfg, new State("s1"), new State("s4")));
//assertFalse(CFLReachability.isReachable(fst,cfg, new State("s1"), new State("s2")));
//assertFalse(CFLReachability.isReachable(fst,cfg, new State("s2"), new State("s4")));
}
public void testAutomatonContainsAllCFL(){
Automaton fst = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a")),
new Transition(new State("s2"), new State("s3"), new Symbol("b")),
new Transition(new State("s2"), new State("s2"), new Symbol("0")),
}
);
ContextFreeGrammar cfg = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b")}),
}
);
assertTrue(CFLReachability.containsAll(fst, cfg));
}
}

View File

@ -0,0 +1,211 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.grammar.string;
import java.util.*;
import com.ibm.wala.automaton.grammar.string.*;
import com.ibm.wala.automaton.string.*;
import junit.framework.TestCase;
public class TestCFLTranslator extends TestCase {
public void testCFLTranslator1() {
Automaton transducer = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("2")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
}
);
ContextFreeGrammar cfg = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0"), new Variable("B")}),
}
);
ContextFreeGrammar expected = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("1"), new Symbol("0"), new Variable("B")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("1"), new Symbol("0"), new Symbol("2")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("1"), new Symbol("2")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0"), new Symbol("2")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("2")}),
}
);
CFLTranslator translator = new CFLTranslator(transducer);
IContextFreeGrammar result = translator.translate(cfg);
assertEquals(expected, result);
}
public void testCFLTranslator2() {
Automaton transducer = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("2")}),
new FilteredTransition(new State("s2"), new State("s2"), new Variable("v"), new Symbol[]{new Variable("v")},
new FilteredTransition.IFilter(){
public List invoke(ISymbol s, List outputs) {
List l = new ArrayList();
for (Iterator i = outputs.iterator(); i.hasNext(); ) {
ISymbol sym = (ISymbol) i.next();
int ival = Integer.parseInt(s.getName());
sym = new Symbol(Integer.toString(ival+1));
l.add(sym);
}
return l;
}
},
new FilteredTransition.ICondition(){
public boolean accept(ISymbol symbol, IMatchContext ctx) {
if (symbol instanceof IVariable) {
return false;
}
try {
Integer.parseInt(symbol.getName());
return true;
} catch(Exception e) {
return false;
}
}
}),
}
);
ContextFreeGrammar cfg = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("2"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("z"), new Variable("B")}),
}
);
ContextFreeGrammar expected = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("1"), new Symbol("1"), new Variable("B")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("1"), new Symbol("1"), new Symbol("2")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("1"), new Symbol("2")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("1"), new Symbol("3"), new Variable("B")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("1"), new Symbol("3"), new Symbol("2")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("2")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("1"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("1"), new Symbol("2")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("3"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("3"), new Symbol("2")}),
}
);
CFLTranslator translator = new CFLTranslator(transducer);
IContextFreeGrammar result = translator.translate(cfg);
assertEquals(expected, result);
}
public void testCFLTranslator3() {
Automaton transducer = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("2")}),
new FilteredTransition(new State("s2"), new State("s2"), new Variable("A"), new Symbol[]{new Variable("A"), new Symbol("z")},
new FilteredTransition.IFilter(){
public List invoke(ISymbol s, List outputs) {
List l = new ArrayList();
ISymbol sym0 = (ISymbol) outputs.get(0);
int ival = Integer.parseInt(s.getName());
sym0 = new Symbol(Integer.toString(ival+1));
l.add(sym0);
ISymbol sym1 = (ISymbol) outputs.get(1);
l.add(sym1);
return l;
}
},
new FilteredTransition.ICondition(){
public boolean accept(ISymbol symbol, IMatchContext ctx) {
if (symbol instanceof IVariable) {
return false;
}
try {
Integer.parseInt(symbol.getName());
return true;
} catch(Exception e) {
return false;
}
}
}),
}
);
ContextFreeGrammar cfg = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("2"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("z"), new Variable("B")}),
}
);
ContextFreeGrammar expected = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("1"), new Symbol("1"), new Symbol("z"), new Variable("B")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("1"), new Symbol("1"), new Symbol("z"), new Symbol("2")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("1"), new Symbol("2")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("1"), new Symbol("3"), new Symbol("z"), new Variable("B")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("1"), new Symbol("3"), new Symbol("z"), new Symbol("2")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("2")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("1"), new Symbol("z"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("1"), new Symbol("z"), new Symbol("2")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("3"), new Symbol("z"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("3"), new Symbol("z"), new Symbol("2")}),
}
);
CFLTranslator translator = new CFLTranslator(transducer);
IContextFreeGrammar result = translator.translate(cfg);
assertEquals(expected, result);
}
public void testCFLTranslator4() {
Automaton transducer = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("2")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
}
);
ContextFreeGrammar cfg = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a")}),
}
);
ContextFreeGrammar expected = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[0]),
}
);
CFLTranslator translator = new CFLTranslator(transducer);
IContextFreeGrammar result = translator.translate(cfg);
assertEquals(expected, result);
}
}

View File

@ -0,0 +1,797 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.grammar.string;
import java.util.*;
import com.ibm.wala.automaton.*;
import com.ibm.wala.automaton.grammar.string.*;
import com.ibm.wala.automaton.string.*;
import junit.framework.TestCase;
public class TestGrammars extends TestCase {
public void testStringValues1() {
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{}),
new ProductionRule(new Variable("B"), new ISymbol[]{new CharSymbol("a")}),
}
);
Set s = Grammars.stringValues(cfg1, new Variable("A"));
assertEquals(null, s);
}
public void testStringValues2() {
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B"), new Variable("C")}),
new ProductionRule(new Variable("C"), new ISymbol[]{}),
new ProductionRule(new Variable("B"), new ISymbol[]{new CharSymbol("a")}),
}
);
Set s = Grammars.stringValues(cfg1, new Variable("A"));
assertEquals(AUtil.set(new String[]{"a"}), s);
}
public void testCreateUnion() {
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("a")}),
}
);
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b")}),
}
);
Map m1 = new HashMap();
Map m2 = new HashMap();
IContextFreeGrammar cfg = Grammars.createUnion(cfg1, cfg2);
assertTrue(CFLReachability.containsSome(cfg1, new ISymbol[]{new Symbol("a"), new Symbol("a")}));
assertFalse(CFLReachability.containsSome(cfg1, new ISymbol[]{new Symbol("b"), new Symbol("b")}));
assertFalse(CFLReachability.containsSome(cfg1, new ISymbol[]{new Symbol("a"), new Symbol("b")}));
assertFalse(CFLReachability.containsSome(cfg2, new ISymbol[]{new Symbol("a"), new Symbol("a")}));
assertTrue(CFLReachability.containsSome(cfg2, new ISymbol[]{new Symbol("b"), new Symbol("b")}));
assertFalse(CFLReachability.containsSome(cfg2, new ISymbol[]{new Symbol("a"), new Symbol("b")}));
assertTrue(CFLReachability.containsSome(cfg, new ISymbol[]{new Symbol("a"), new Symbol("a")}));
assertTrue(CFLReachability.containsSome(cfg, new ISymbol[]{new Symbol("b"), new Symbol("b")}));
assertFalse(CFLReachability.containsSome(cfg, new ISymbol[]{new Symbol("a"), new Symbol("b")}));
assertFalse(CFLReachability.containsSome(cfg, new ISymbol[]{new Symbol("b"), new Symbol("a")}));
}
public void testCreateConcat() {
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("a")}),
}
);
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b")}),
}
);
Map m1 = new HashMap();
Map m2 = new HashMap();
IContextFreeGrammar cfg = Grammars.createConcatenation(cfg1, cfg2);
assertTrue(CFLReachability.containsSome(cfg1, new ISymbol[]{new Symbol("a"), new Symbol("a")}));
assertFalse(CFLReachability.containsSome(cfg1, new ISymbol[]{new Symbol("b"), new Symbol("b")}));
assertFalse(CFLReachability.containsSome(cfg1, new ISymbol[]{new Symbol("a"), new Symbol("b")}));
assertFalse(CFLReachability.containsSome(cfg2, new ISymbol[]{new Symbol("a"), new Symbol("a")}));
assertTrue(CFLReachability.containsSome(cfg2, new ISymbol[]{new Symbol("b"), new Symbol("b")}));
assertFalse(CFLReachability.containsSome(cfg2, new ISymbol[]{new Symbol("a"), new Symbol("b")}));
assertTrue(CFLReachability.containsSome(cfg, new ISymbol[]{new Symbol("a"), new Symbol("a")}));
assertTrue(CFLReachability.containsSome(cfg, new ISymbol[]{new Symbol("b"), new Symbol("b")}));
assertTrue(CFLReachability.containsSome(cfg, new ISymbol[]{new Symbol("a"), new Symbol("b")}));
assertFalse(CFLReachability.containsSome(cfg, new ISymbol[]{new Symbol("b"), new Symbol("a")}));
}
public void testEliminateEpsilonRules(){
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("+"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("-"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("("), new Variable("A"), new Symbol(")")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("1")}),
new ProductionRule(new Variable("B"), new ISymbol[]{}),
}
);
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("+"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("+")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("-"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("-")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B")}),
new ProductionRule(new Variable("A"), new ISymbol[]{}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("("), new Variable("A"), new Symbol(")")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("("), new Symbol(")")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("1")}),
}
);
assertFalse(cfg1.equals(cfg2));
Grammars.eliminateEpsilonRules(cfg1);
assertEquals(cfg2, cfg1);
}
public void testEliminateEpsilonRules2(){
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a")}),
new ProductionRule(new Variable("A"), new ISymbol[]{}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Variable("A")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b")}),
new ProductionRule(new Variable("B"), new ISymbol[]{}),
}
);
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a")}),
new ProductionRule(new Variable("A"), new ISymbol[]{}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Variable("A")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b")}),
}
);
assertFalse(cfg1.equals(cfg2));
Grammars.eliminateEpsilonRules(cfg1);
assertEquals(cfg2, cfg1);
}
public void testEliminateEpsilonRules3(){
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Variable("C")}),
new ProductionRule(new Variable("C"), new ISymbol[]{}),
}
);
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Variable("C")}),
}
);
// TODO: should eliminate the dangling rules.
/*
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{}),
}
);
*/
assertFalse(cfg1.equals(cfg2));
Grammars.eliminateEpsilonRules(cfg1);
assertEquals(cfg2, cfg1);
}
public void testEliminateEpsilonRules4(){
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B")}),
new ProductionRule(new Variable("A"), new ISymbol[]{}),
new ProductionRule(new Variable("B"), new ISymbol[]{}),
}
);
// TODO: should eliminate the dangling rules.
/*
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{}),
}
);
*/
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B")}),
}
);
assertFalse(cfg1.equals(cfg2));
Grammars.eliminateEpsilonRules(cfg1);
assertEquals(cfg2, cfg1);
}
public void testEliminateUnitRules(){
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("+"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("+")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("-"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("-")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B")}),
new ProductionRule(new Variable("A"), new ISymbol[]{}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("("), new Variable("A"), new Symbol(")")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("("), new Symbol(")")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("1")}),
}
);
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("+"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("+")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("-"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("-")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("("), new Variable("A"), new Symbol(")")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("("), new Symbol(")")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("1")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("("), new Variable("A"), new Symbol(")")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("("), new Symbol(")")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("1")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("1")}),
new ProductionRule(new Variable("A"), new ISymbol[]{}),
}
);
assertFalse(cfg1.equals(cfg2));
Grammars.eliminateUnitRules(cfg1);
assertEquals(cfg2, cfg1);
}
public void testEliminateUnitRules2(){
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("+"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("-"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("("), new Variable("A"), new Symbol(")")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("1")}),
new ProductionRule(new Variable("B"), new ISymbol[]{}),
}
);
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("+"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("-"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("("), new Variable("A"), new Symbol(")")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("1")}),
new ProductionRule(new Variable("A"), new ISymbol[]{}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("("), new Variable("A"), new Symbol(")")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("1")}),
new ProductionRule(new Variable("B"), new ISymbol[]{}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("1")}),
new ProductionRule(new Variable("A"), new ISymbol[]{}),
}
);
assertFalse(cfg1.equals(cfg2));
Grammars.eliminateUnitRules(cfg1);
assertEquals(cfg2, cfg1);
}
public void testEliminateUnitRules3(){
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("+"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Variable("C")}),
new ProductionRule(new Variable("C"), new ISymbol[]{new Symbol("0")}),
}
);
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("+"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("C"), new ISymbol[]{new Symbol("0")}),
}
);
assertFalse(cfg1.equals(cfg2));
Grammars.eliminateUnitRules(cfg1);
assertEquals(cfg2, cfg1);
}
public void testEliminateUnitRules4(){
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a")}),
new ProductionRule(new Variable("A"), new ISymbol[]{}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Variable("A")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b")}),
new ProductionRule(new Variable("B"), new ISymbol[]{}),
}
);
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("b")}),
new ProductionRule(new Variable("A"), new ISymbol[]{}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("a")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b")}),
new ProductionRule(new Variable("B"), new ISymbol[]{}),
}
);
assertFalse(cfg1.equals(cfg2));
Grammars.eliminateUnitRules(cfg1);
assertEquals(cfg2, cfg1);
}
public void testEliminateUnitRules5(){
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Variable("A")}),
}
);
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
}
);
assertFalse(cfg1.equals(cfg2));
Grammars.eliminateUnitRules(cfg1);
assertEquals(cfg2, cfg1);
}
public void testEliminateUnitRules6(){
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Variable("A")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b")}),
}
);
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("b")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b")}),
}
);
assertFalse(cfg1.equals(cfg2));
Grammars.eliminateUnitRules(cfg1);
assertEquals(cfg2, cfg1);
}
public void testSimplifyRules(){
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("+"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("+")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("-"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("-")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("("), new Variable("A"), new Symbol(")")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("("), new Symbol(")")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("1")}),
new ProductionRule(new Variable("A"), new ISymbol[]{}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("("), new Variable("A"), new Symbol(")")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("("), new Symbol(")")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("1")}),
}
);
Grammars.simplifyRules(cfg1, null);
Set strs = AUtil.set(new String[]{"N1","N2","N3","N4"});
Set comb = AUtil.allOrder(strs);
boolean result = false;
for (Iterator i = comb.iterator(); i.hasNext(); ) {
List l = (List) i.next();
String n1 = (String) l.get(0);
String n2 = (String) l.get(1);
String n3 = (String) l.get(2);
String n4 = (String) l.get(3);
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Variable(n1)}),
new ProductionRule(new Variable(n1), new ISymbol[]{new Symbol("+"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("+")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Variable(n2)}),
new ProductionRule(new Variable(n2), new ISymbol[]{new Symbol("-"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("-")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("("), new Variable(n3)}),
new ProductionRule(new Variable(n3), new ISymbol[]{new Variable("A"), new Symbol(")")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("("), new Symbol(")")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("1")}),
new ProductionRule(new Variable("A"), new ISymbol[]{}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("("), new Variable(n4)}),
new ProductionRule(new Variable(n4), new ISymbol[]{new Variable("A"), new Symbol(")")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("("), new Symbol(")")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("1")}),
}
);
if (cfg2.equals(cfg1)) {
result = true;
break;
}
}
assertTrue(result);
}
public void testSimplifyRules2(){
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("+"), new Variable("A"), new Variable("B")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("+")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol(";")}),
}
);
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Variable("N1")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("+")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol(";")}),
new ProductionRule(new Variable("N1"), new ISymbol[]{new Symbol("+"), new Variable("N2")}),
new ProductionRule(new Variable("N2"), new ISymbol[]{new Variable("A"), new Variable("B")}),
}
);
assertFalse(cfg1.equals(cfg2));
Grammars.simplifyRules(cfg1, null);
assertEquals(cfg2, cfg1);
}
public void testMoveTerminalsToUnitRules(){
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Variable("N1")}),
new ProductionRule(new Variable("N1"), new ISymbol[]{new Symbol("+"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("+")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Variable("N2")}),
new ProductionRule(new Variable("N2"), new ISymbol[]{new Symbol("-"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("-")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("("), new Variable("N3")}),
new ProductionRule(new Variable("N3"), new ISymbol[]{new Variable("A"), new Symbol(")")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("("), new Symbol(")")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("1")}),
new ProductionRule(new Variable("A"), new ISymbol[]{}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("("), new Variable("N4")}),
new ProductionRule(new Variable("N4"), new ISymbol[]{new Variable("A"), new Symbol(")")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("("), new Symbol(")")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("1")}),
}
);
Grammars.moveTerminalsToUnitRules(cfg1, null);
Set strs = AUtil.set(new String[]{"N5","N6","N7","N8"});
Set comb = AUtil.allOrder(strs);
boolean result = false;
for (Iterator i = comb.iterator(); i.hasNext(); ) {
List l = (List) i.next();
String plus = (String) l.get(0);
String minus = (String) l.get(1);
String lparen = (String) l.get(2);
String rparen = (String) l.get(3);
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Variable("N1")}),
new ProductionRule(new Variable("N1"), new ISymbol[]{new Variable(plus), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("+")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Variable("N2")}),
new ProductionRule(new Variable("N2"), new ISymbol[]{new Variable(minus), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("-")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable(lparen), new Variable("N3")}),
new ProductionRule(new Variable("N3"), new ISymbol[]{new Variable("A"), new Variable(rparen)}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable(lparen), new Variable(rparen)}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("1")}),
new ProductionRule(new Variable("A"), new ISymbol[]{}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Variable(lparen), new Variable("N4")}),
new ProductionRule(new Variable("N4"), new ISymbol[]{new Variable("A"), new Variable(rparen)}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Variable(lparen), new Variable(rparen)}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("1")}),
new ProductionRule(new Variable(plus), new ISymbol[]{new Symbol("+")}),
new ProductionRule(new Variable(minus), new ISymbol[]{new Symbol("-")}),
new ProductionRule(new Variable(lparen), new ISymbol[]{new Symbol("(")}),
new ProductionRule(new Variable(rparen), new ISymbol[]{new Symbol(")")}),
}
);
if (cfg2.equals(cfg1)) {
result = true;
break;
}
}
assertTrue(result);
}
public void testMoveTerminalsToUnitRules2(){
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("+"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("0")}),
}
);
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Variable("N1"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("N1"), new ISymbol[]{new Symbol("+")}),
}
);
Grammars.moveTerminalsToUnitRules(cfg1, null);
List l1 = AUtil.sort(cfg1.getRules());
IProductionRule r11 = (IProductionRule) l1.get(0);
IProductionRule r12 = (IProductionRule) l1.get(1);
IProductionRule r13 = (IProductionRule) l1.get(2);
List l2 = AUtil.sort(cfg2.getRules());
IProductionRule r21 = (IProductionRule) l2.get(0);
IProductionRule r22 = (IProductionRule) l2.get(1);
IProductionRule r23 = (IProductionRule) l2.get(2);
assertEquals(r11, r21);
assertEquals(r12, r22);
assertEquals(r13, r23);
assertEquals(r21, r11);
assertEquals(r22, r12);
assertEquals(r23, r13);
assertEquals(l1, l2);
assertTrue(cfg2.equals(cfg1));
assertTrue(cfg1.equals(cfg2));
}
public void testEliminateUselessRules() {
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("+"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("-"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Variable("("), new Variable("A"), new Variable(")")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0")}),
new ProductionRule(new Variable("C"), new ISymbol[]{new Variable("D")}),
new ProductionRule(new Variable("D"), new ISymbol[]{new Symbol("0")}),
}
);
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("+"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("-"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Variable("("), new Variable("A"), new Variable(")")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("0")}),
}
);
Grammars.eliminateUselessRules(cfg1);
assertEquals(cfg2, cfg1);
}
public void testEliminateDanglingVariables1() {
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("+"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("-"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("N")}),
}
);
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("+"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("-"), new Variable("A")}),
}
);
Grammars.eliminateDanglingVariables(cfg1);
assertEquals(cfg2, cfg1);
}
public void testEliminateDanglingVariables2() {
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("+"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("-"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("N")}),
}
);
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("+"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("-"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("N")}),
}
);
Grammars.eliminateDanglingVariables(cfg1, AUtil.set(new Variable[]{new Variable("N")}));
assertEquals(cfg2, cfg1);
}
public void testCollectLinearRules() {
ContextFreeGrammar cfg = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("a")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("a"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a"), new Variable("A")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a")}),
}
);
Set el = AUtil.set(new ProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("a")}),
});
Set er = AUtil.set(new ProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a"), new Variable("A")}),
});
Set es = AUtil.set(new ProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("A"), new Symbol("a")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a"), new Variable("A")}),
});
Set l = new HashSet();
Set r = new HashSet();
Grammars.collectLinearRules(cfg, l, r);
assertEquals(el, l);
assertEquals(er, r);
assertEquals(el, Grammars.collectLeftLinearRules(cfg));
assertEquals(er, Grammars.collectRightLinearRules(cfg));
assertEquals(es, Grammars.collectLinearRules(cfg));
}
public void testCollectMutuallyRecursiveVariables() {
ContextFreeGrammar cfg = new ContextFreeGrammar(
new Variable("A"),
new IProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Variable("B"), new Symbol("+"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Variable("C"), new Symbol("*"), new Variable("C")}),
new ProductionRule(new Variable("C"), new ISymbol[]{new Variable("N")}),
new ProductionRule(new Variable("C"), new ISymbol[]{new Variable("D")}),
new ProductionRule(new Variable("C"), new ISymbol[]{new Symbol("("), new Variable("A"), new Symbol(")")}),
new ProductionRule(new Variable("N"), new ISymbol[]{new Symbol("0"), new Variable("N")}),
new ProductionRule(new Variable("N"), new ISymbol[]{}),
new ProductionRule(new Variable("D"), new ISymbol[]{new Symbol("["), new Variable("N"), new Symbol("]")}),
}
);
Set expected = AUtil.set(new Variable[]{new Variable("A"), new Variable("B"), new Variable("C"), new Variable("N")});
Set mvars = Grammars.collectMutuallyRecursiveVariables(cfg);
assertEquals(expected, mvars);
Set expectedEx = AUtil.set(new Variable[]{new Variable("A"), new Variable("B"), new Variable("C")});
Set mvarsEx = Grammars.RegularApproximation.collectMutuallyRecursiveVariablesEx(cfg);
assertEquals(expectedEx, mvarsEx);
}
/**
* use the example described in the following literature.
* M. Mohri: "Regular Approximation of Context-Free Grammars Through Transformation"
*/
public void testRegularApproximation() {
ContextFreeGrammar cfg = new ContextFreeGrammar(
new Variable("E"),
new IProductionRule[]{
new ProductionRule(new Variable("E"), new ISymbol[]{new Variable("E"), new Symbol("+"), new Variable("T")}),
new ProductionRule(new Variable("E"), new ISymbol[]{new Variable("T")}),
new ProductionRule(new Variable("T"), new ISymbol[]{new Variable("T"), new Symbol("*"), new Variable("F")}),
new ProductionRule(new Variable("T"), new ISymbol[]{new Variable("F")}),
new ProductionRule(new Variable("F"), new ISymbol[]{new Symbol("("), new Variable("E"), new Symbol(")")}),
new ProductionRule(new Variable("F"), new ISymbol[]{new Symbol("a")}),
}
);
Grammars.RegularApproximation.approximateToRegular(cfg);
IAutomaton expected = new Automaton(
new State("s0"),
new State[]{new State("s1")},
new Transition[]{
new Transition(new State("s0"), new State("s0"), new Symbol("(")),
new Transition(new State("s0"), new State("s1"), new Symbol("a")),
new Transition(new State("s1"), new State("s1"), new Symbol(")")),
new Transition(new State("s1"), new State("s0"), new Symbol("*")),
new Transition(new State("s1"), new State("s0"), new Symbol("+")),
}
);
boolean result = CFLReachability.containsAll(expected, cfg);
assertTrue(result);
}
public void testToCFG1() {
IAutomaton fst = new Automaton(
new State("s0"),
new State[]{new State("s1")},
new Transition[]{
new Transition(new State("s0"), new State("s0"), new Symbol("(")),
new Transition(new State("s0"), new State("s1"), new Symbol("a")),
new Transition(new State("s1"), new State("s1"), new Symbol(")")),
new Transition(new State("s1"), new State("s0"), new Symbol("*")),
new Transition(new State("s1"), new State("s0"), new Symbol("+")),
}
);
IContextFreeGrammar cfg = Grammars.toCFG(fst);
boolean result = false;
//System.out.println(Automatons.toGraphviz(fst));
result = CFLReachability.containsSome(cfg, fst);
assertTrue(result);
//System.out.println(Automatons.toGraphviz(fst));
result = CFLReachability.containsAll(fst, cfg);
assertTrue(result);
//System.out.println(Automatons.toGraphviz(fst));
result = CFLReachability.containsSome(cfg, fst);
assertTrue(result);
}
}

View File

@ -0,0 +1,126 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.grammar.tree;
import java.util.*;
import com.ibm.wala.automaton.*;
import com.ibm.wala.automaton.grammar.tree.*;
import com.ibm.wala.automaton.string.*;
import com.ibm.wala.automaton.test.tree.TreeJunitBase;
import com.ibm.wala.automaton.tree.*;
public class TestRTLBottomUpTranslator extends TreeJunitBase {
/*
* null -> <s1>(null)
*
* u2[<s1>(x), <s1>(y)] -> <s2>(t2[x,y])
*
* u1[<s1>(x), <s1>(y)] -> <s1>(u1[x, y])
* u1[<s1>(x), <s2>(y)] -> <s3>(t1[x, y])
* u1[<s1>(x), <s3>(y)] -> <s3>(y)
*
* u1[<s2>(x), <s1>(y)] -> <s3>(t1[x, y])
* u1[<s2>(x), <s2>(y)] -> <s3>(t1[x, y])
* u1[<s2>(x), <s3>(y)] -> <s3>(y)
*
* u1[<s3>(x), <s1>(y)] -> <s3>(x)
* u1[<s3>(x), <s2>(y)] -> <s3>(x)
* u1[<s3>(x), <s3>(y)] -> <s3>(x)
* u1[<s3>(x), <s3>(y)] -> <s3>(y)
*/
public void testRTLBottomUpTranslator() {
State s1 = new State("s1");
State s2 = new State("s2");
State s3 = new State("s3");
BinaryTreeVariable x = new BinaryTreeVariable("x");
BinaryTreeVariable y = new BinaryTreeVariable("y");
BottomUpTreeAutomaton butt = new BottomUpTreeAutomaton(
new State[]{s3},
new TreeTransition[]{
new TreeTransition(
BinaryTree.LEAF,
new StateBinaryTree(s1, BinaryTree.LEAF)
),
new TreeTransition(
new BinaryTree("u2", new StateBinaryTree(s1, x), new StateBinaryTree(s1, y)),
new StateBinaryTree(s2, new BinaryTree("t2", x, y))
),
new TreeTransition(
new BinaryTree("u1", new StateBinaryTree(s1, x), new StateBinaryTree(s1, y)),
new StateBinaryTree(s1, new BinaryTree("u1", x, y))
),
new TreeTransition(
new BinaryTree("u1", new StateBinaryTree(s1, x), new StateBinaryTree(s2, y)),
new StateBinaryTree(s3, new BinaryTree("t1", x, y))
),
new TreeTransition(
new BinaryTree("u1", new StateBinaryTree(s1, x), new StateBinaryTree(s3, y)),
new StateBinaryTree(s3, y)
),
new TreeTransition(
new BinaryTree("u1", new StateBinaryTree(s2, x), new StateBinaryTree(s1, y)),
new StateBinaryTree(s3, new BinaryTree("t1", x, y))
),
new TreeTransition(
new BinaryTree("u1", new StateBinaryTree(s2, x), new StateBinaryTree(s2, y)),
new StateBinaryTree(s3, new BinaryTree("t1", x, y))
),
new TreeTransition(
new BinaryTree("u1", new StateBinaryTree(s2, x), new StateBinaryTree(s3, y)),
new StateBinaryTree(s3, y)
),
new TreeTransition(
new BinaryTree("u1", new StateBinaryTree(s3, x), new StateBinaryTree(s1, y)),
new StateBinaryTree(s3, x)
),
new TreeTransition(
new BinaryTree("u1", new StateBinaryTree(s3, x), new StateBinaryTree(s2, y)),
new StateBinaryTree(s3, x)
),
new TreeTransition(
new BinaryTree("u1", new StateBinaryTree(s3, x), new StateBinaryTree(s3, y)),
new StateBinaryTree(s3, x)
),
new TreeTransition(
new BinaryTree("u1", new StateBinaryTree(s3, x), new StateBinaryTree(s3, y)),
new StateBinaryTree(s3, y)
),
}
);
RTLBottomUpTranslator translator = new RTLBottomUpTranslator(butt);
ITreeGrammar g = TG(
BV("v1"),
new Object[]{
BV("v1"), BT("u1", BV("v2"), BV("v3")),
BV("v2"), BT("u1", BT("u2"), BV("v2")),
BV("v2"), BT("u1"),
BV("v3"), BT("u1"),
}
);
ITreeGrammar ex1 = TG(
BV("v1"),
new Object[]{
BV("v1"), BT("t1", BT("t2"), BT("t2")),
}
);
ITreeGrammar result = translator.translate(g);
TreeGrammars.eliminateDanglingVariables(result);
assertContains(ex1, result);
assertContains(result, ex1);
}
}

View File

@ -0,0 +1,304 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.grammar.tree;
import com.ibm.wala.automaton.grammar.tree.*;
import com.ibm.wala.automaton.parser.*;
import com.ibm.wala.automaton.regex.string.*;
import com.ibm.wala.automaton.string.CharSymbol;
import com.ibm.wala.automaton.test.tree.TreeJunitBase;
public class TestRTLComparator extends TreeJunitBase {
/*
g = {
$A -> ();
$A -> a((),$A);
$A -> a((),$B);
$B -> ();
$B -> b((),$B);
}
h = {
$AB -> ();
$AB -> a((),$AB);
$AB -> b((),$AB);
}
*/
public void testRTLComparator1() {
ITreeGrammar g = TG(
BV("A"),
new Object[]{
BV("A"), BT("a", null, BV("A")),
BV("A"), BT("a", null, BV("B")),
BV("B"), null,
BV("B"), BT("b", null, BV("B")),
}
);
ITreeGrammar h = TG(
BV("AB"),
new Object[]{
BV("AB"), null,
BV("AB"), BT("a", null, BV("AB")),
BV("AB"), BT("b", null, BV("AB")),
}
);
assertContains(h, g);
assertNotContains(g, h);
}
/*
g = {
$A -> ();
$A -> a((),$A);
$A -> a((),$B);
$B -> ();
$B -> c((),$B);
}
h = {
$AB -> ();
$AB -> a((),$AB);
$AB -> b((),$AB);
}
*/
public void testRTLComparator2() {
ITreeGrammar g = TG(
BV("A"),
new Object[]{
BV("A"), null,
BV("A"), BT("a", null, BV("A")),
BV("A"), BT("a", null, BV("B")),
BV("B"), null,
BV("B"), BT("c", null, BV("B")),
}
);
ITreeGrammar h = TG(
BV("AB"),
new Object[]{
BV("AB"), null,
BV("AB"), BT("a", null, BV("AB")),
BV("AB"), BT("b", null, BV("AB")),
}
);
assertNotContains(g, h);
assertNotContains(h, g);
}
/*
Grammar g3 = {
$G -> a((),());
}
Grammar h3 = {
$G -> a((),());
}
*/
public void testRTLComparator3() {
ITreeGrammar g = TG(
BV("G"),
new Object[]{
BV("G"), BT("a"),
}
);
ITreeGrammar h = TG(
BV("G"),
new Object[]{
BV("G"), BT("a"),
}
);
assertContains(g, h);
assertContains(h, g);
}
/*
Grammar f4 = {
$G -> l(a((),()),());
}
Grammar h4 = {
$G -> l(b((),()),());
}
*/
public void testRTLComparator4() {
ITreeGrammar g = TG(
BV("G"),
new Object[]{
BV("G"), BT("l", BT("a"), null),
}
);
ITreeGrammar h = TG(
BV("G"),
new Object[]{
BV("G"), BT("l", BT("a"), null),
}
);
assertContains(g, h);
assertContains(h, g);
}
/*
Grammar g5 = {
$G -> b(b((),()),());
}
Grammar h5 = {
$G -> a($G,());
$G -> b($G,());
$G -> ();
}
*/
public void testRTLComparator5() {
ITreeGrammar g = TG(
BV("G"),
new Object[]{
BV("G"), BT("b", BT("b"), null),
}
);
ITreeGrammar h = TG(
BV("G"),
new Object[]{
BV("G"), BT("a", BV("G"), null),
BV("G"), BT("b", BV("G"), null),
BV("G"), null,
}
);
assertNotContains(g, h);
assertContains(h, g);
}
/*
Grammar g6 = {
$G -> a(b((),c((),())), ());
}
Grammar h6 = {
$G -> a($H, ());
$H -> b((),c((),()));
$H -> b((),());
}
*/
public void testRTLComparator6() {
ITreeGrammar g = TG(
BV("G"),
new Object[]{
BV("G"), BT("a", BT("b", null, BT("c")), null),
}
);
ITreeGrammar h = TG(
BV("G"),
new Object[]{
BV("G"), BT("a", BV("H"), null),
BV("H"), BT("b", null, BT("c")),
BV("H"), BT("b"),
}
);
assertNotContains(g, h);
assertContains(h, g);
}
/*
* g = {
* $A -> a($C,$B)
* $B -> b((),())
* $C -> c((),())
* }
*
* h = {
* $A -> a($C,$B)
* $B -> $BB
* $C -> $CC
* $BB -> b((),())
* $CC -> c((),())
* }
*/
public void testRTLComparator7() {
ITreeGrammar g = TG(
BV("A"),
new Object[]{
BV("A"), BT("a", BV("B"), BV("C")),
BV("B"), BT("b"),
BV("C"), BT("c"),
}
);
ITreeGrammar h = TG(
BV("A"),
new Object[]{
BV("A"), BT("a", BV("B"), BV("C")),
BV("B"), BV("BB"),
BV("C"), BV("CC"),
BV("BB"), BT("b"),
BV("CC"), BT("c"),
}
);
assertContains(g, h);
assertContains(h, g);
}
/*
* g = {
* $G -> ();
* }
*
* h = {
* $G -> a[];
* }
*/
public void testRTLComparator8() {
ITreeGrammar g = TG(
BV("G"),
new Object[]{
BV("G"), null,
}
);
ITreeGrammar h = TG(
BV("G"),
new Object[]{
BV("G"), BT("a"),
}
);
assertNotContains(g, h);
assertNotContains(h, g);
}
/*
* g = {
* $A -> "a|A"($B, $C)
* $B -> "b|B"((),())
* $C -> "c|C"((),())
* }
* h = {
* $A -> a($B, $C)
* $B -> b((),())
* $C -> c((),())
* }
*/
public void testRTLComparatorWithPattern1() {
StringPatternSymbol a = new StringPatternSymbol("a|A");
StringPatternSymbol b = new StringPatternSymbol("b|B");
StringPatternSymbol c = new StringPatternSymbol("c|C");
ITreeGrammar g = TG(
BV("A"),
new Object[]{
BV("A"), BT(a, BV("B"), BV("C")),
BV("B"), BT(b),
BV("C"), BT(c),
}
);
ITreeGrammar h = TG(
BV("A"),
new Object[]{
BV("A"), BT(new CharSymbol("a"), BV("B"), BV("C")),
BV("B"), BT(new CharSymbol("b")),
BV("C"), BT(new CharSymbol("c")),
}
);
assertContains(g, h);
assertNotContains(h, g);
}
}

View File

@ -0,0 +1,82 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.grammar.tree;
import com.ibm.wala.automaton.AUtil;
import com.ibm.wala.automaton.grammar.string.*;
import com.ibm.wala.automaton.grammar.tree.*;
import com.ibm.wala.automaton.string.*;
import com.ibm.wala.automaton.test.tree.TreeJunitBase;
import com.ibm.wala.automaton.tree.*;
public class TestRTLTopDownTranslator extends TreeJunitBase {
public void testRTLTopDownTranslator1() {
ITreeGrammar tg = TG(
BV(1),
new Object[]{
BV(1), BT("a", BV(2), BT("c")),
BV(2), BT("b"),
}
);
State s1 = new State("s1");
State s2 = new State("s2");
State s3 = new State("s3");
BinaryTreeVariable x = new BinaryTreeVariable("x");
BinaryTreeVariable y = new BinaryTreeVariable("y");
TopDownTreeAutomaton tdtt = new TopDownTreeAutomaton(
s1,
new TreeTransition[]{
new TreeTransition(
new StateBinaryTree(s1, new BinaryTree("a", x, y)),
new BinaryTree("A", new StateBinaryTree(s3, y), new StateBinaryTree(s2, x))
),
new TreeTransition(
new StateBinaryTree(s2, new BinaryTree("b", x, y)),
new BinaryTree("B")
),
new TreeTransition(
new StateBinaryTree(s3, new BinaryTree("c", x, y)),
new BinaryTree("c")
),
new TreeTransition(
new StateBinaryTree(s3, new BinaryTree("c", x, y)),
new BinaryTree("C")
),
}
);
ITreeGrammar spec1 = TG(
BV("A"),
new Object[]{
BV("A"), BT("A", BV("C"), BV("B")),
BV("B"), BT("B"),
BV("C"), BT("c"),
BV("C"), BT("C"),
}
);
ITreeGrammar spec2 = TG(
BV("A"),
new Object[]{
BV("A"), BT("A", BV("C"), BV("B")),
BV("B"), BT("B"),
BV("C"), BT("C"),
}
);
RTLTopDownTranslator translator = new RTLTopDownTranslator(tdtt);
ITreeGrammar result = translator.translate(tg);
assertContains(spec1, result);
assertContains(result, spec1);
assertNotContains(spec2, result);
assertContains(result, spec2);
}
}

View File

@ -0,0 +1,57 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.grammar.tree;
import com.ibm.wala.automaton.grammar.string.*;
import com.ibm.wala.automaton.grammar.tree.*;
import com.ibm.wala.automaton.string.Symbol;
import com.ibm.wala.automaton.string.Variable;
import com.ibm.wala.automaton.test.tree.TreeJunitBase;
public class TestTreeGrammar extends TreeJunitBase {
public void testTreeGrammarNew() {
ITreeGrammar tg = TG(
BV(1),
new Object[]{
BV(1), BT("a", BV(2), BT("c")),
BV(2), BT("b", BV(2), null),
}
);
}
public void testTreeGrammarNew2() {
Variable v1 = new Variable("v1");
Variable v2 = new Variable("v2");
Variable v3 = new Variable("v3");
SimpleGrammar g = new SimpleGrammar(
v1,
new IProductionRule[]{
new ProductionRule(v1, BT("a", BV("v2"), BV("v3"))),
new ProductionRule(v2, BT("b", BV("v3"), null)),
new ProductionRule(v3, new Symbol("c")),
});
ITreeGrammar tg = new TreeGrammar(g);
IContextFreeGrammar cfg = new ContextFreeGrammar(
v3,
new IProductionRule[]{
new ProductionRule(v3, new Symbol("c")),
});
ITreeGrammar expected = TG(
BV("v1"),
new Object[]{
BV("v1"), BT("a", BV("v2"), BV("v3")),
BV("v2"), BT("b", BV("v3"), null),
BV("v3"), BT(new CFGSymbol(cfg), null, null),
}
);
assertEquals(expected, tg);
}
}

View File

@ -0,0 +1,138 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.grammar.tree;
import com.ibm.wala.automaton.AUtil;
import com.ibm.wala.automaton.grammar.string.*;
import com.ibm.wala.automaton.grammar.tree.*;
import com.ibm.wala.automaton.string.DeepSymbolCopier;
import com.ibm.wala.automaton.string.ISymbol;
import com.ibm.wala.automaton.test.tree.TreeJunitBase;
import com.ibm.wala.automaton.tree.*;
public class TestTreeGrammars extends TreeJunitBase {
public void testTreeGrammarsNormalize1() {
ITreeGrammar tg = TG(
BV(1),
new Object[]{
BV(1), BT("a", BV(2), BT("c")),
BV(2), BT("b", BV(2), BinaryTree.LEAF),
}
);
ITreeGrammar ex = TG(
BV(1),
new Object[]{
BV(1), BT("a", BV(2), BV("N2")),
BV(2), BT("b", BV(2), BV("N1")),
BV("N1"), BinaryTree.LEAF,
BV("N2"), BT("c", BV("N1"), BV("N1")),
}
);
TreeGrammars.normalize(tg);
assertEquals(ex, tg);
}
public void testTreeGrammarsNormalize2() {
ITreeGrammar tg = TG(
BV(1),
new Object[]{
BV(1), BT("a", BV(2), BT("c")),
BV(2), BT("b", BV(2), BinaryTree.LEAF),
BV(2), BV(2),
}
);
ITreeGrammar ex = TG(
BV(1),
new Object[]{
BV(1), BT("a", BV(2), BV("N2")),
BV(2), BT("b", BV(2), BV("N1")),
BV("N1"), BinaryTree.LEAF,
BV("N2"), BT("c", BV("N1"), BV("N1")),
}
);
TreeGrammars.normalize(tg);
assertEquals(ex, tg);
}
public void testTreeGrammarAppend1() {
ITreeGrammar tg = TG(
BV(1),
new Object[]{
BV(1), BT("a", BV(2), BT("c")),
BV(2), BT("b", BV(2), BinaryTree.LEAF),
BV(2), BT("c"),
BV(2), BV(2),
}
);
ITreeGrammar ex = TG(
BV(1),
new Object[]{
BV(1), BT("a", BV(2), BT("c", null, BT("z"))),
BV(2), BT("b", BV(2), BinaryTree.LEAF),
BV(2), BT("c"),
BV(2), BV(2),
}
);
TreeGrammars.append(tg, BT("z"));
assertContains(ex, tg);
assertContains(tg, ex);
}
public void testTreeGrammarAppend2() {
ITreeGrammar tg = TG(
BV(1),
new Object[]{
BV(1), BT("a", BV(2), null),
BV(2), BT("b", BV(2), BinaryTree.LEAF),
BV(2), BT("c"),
BV(2), BV(2),
}
);
ITreeGrammar ex = TG(
BV(1),
new Object[]{
BV(1), BT("a", BV(2), BT("z")),
BV(2), BT("b", BV(2), BinaryTree.LEAF),
BV(2), BT("c"),
BV(2), BV(2),
}
);
TreeGrammars.append(tg, BT("z"));
assertContains(ex, tg);
assertContains(tg, ex);
}
public void testTreeGrammarAppendChild1() {
ITreeGrammar tg = TG(
BV(1),
new Object[]{
BV(1), BT("a", BV(2), BT("c")),
BV(2), BT("b", BV(2), BinaryTree.LEAF),
BV(2), BT("c"),
BV(2), BV(2),
}
);
ITreeGrammar ex = TG(
BV(1),
new Object[]{
BV(1), BT("a", BV(2), BT("c")),
BV(2), BT("b", BV(3), BT("z")),
BV(2), BT("c", null, BT("z")),
BV(2), BT("b", BV(3), BT("z")),
BV(3), BT("b", BV(3), BinaryTree.LEAF),
BV(3), BT("c"),
}
);
TreeGrammars.appendChild(tg, BT("z"));
assertContains(ex, tg);
assertContains(tg, ex);
}
}

View File

@ -0,0 +1,552 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.parser;
import java.util.*;
import com.ibm.wala.automaton.AUtil;
import com.ibm.wala.automaton.grammar.string.ProductionRule;
import com.ibm.wala.automaton.grammar.tree.*;
import com.ibm.wala.automaton.parser.AmtParser;
import com.ibm.wala.automaton.regex.string.*;
import com.ibm.wala.automaton.string.*;
import com.ibm.wala.automaton.tree.*;
import junit.framework.TestCase;
public class TestAmtParser extends TestCase {
private AmtParser parser;
public void setUp() {
parser = new AmtParser();
parser.parse("search com.ibm.wala.automaton.string");
}
public void verifyVariable(IVariable v, Object obj) {
assertEquals(obj, parser.getResult().get(v));
}
public void verifyVariable(String v, Object obj) {
verifyVariable(new Variable(v), obj);
}
public void testSymbol() {
parser.parse("$x = a");
verifyVariable("x", new StringSymbol("a"));
}
public void testVariableRef() {
parser.parse("$x = a; $y = $x");
verifyVariable("x", new StringSymbol("a"));
}
public void testVariable() {
parser.parse("$x = a; $y = $$x");
verifyVariable("y", new Variable("x"));
}
public void testPrefixedSymbol() {
Map m = parser.parse("$x = prefix:foo");
verifyVariable("x", new PrefixedSymbol(new StringSymbol("prefix"), new StringSymbol("foo")));
}
public void testInstantiation() {
parser.parse("$x = Symbol(\"a\")");
verifyVariable("x", new Symbol("a"));
}
public void testInstantiationWithAlias() {
parser.parse("alias S = StringSymbol");
parser.parse("$x = S(\"a\")");
parser.parse("alias S = NumberSymbol");
parser.parse("$n = S(123)");
parser.parse("delete alias S");
verifyVariable("x", new StringSymbol("a"));
verifyVariable("n", new NumberSymbol(123));
}
public void testInstantiationWithProps() {
parser.parse("$x = FlexibleNamingSymbol(\"a\"){ name = \"b\" }");
verifyVariable("x", new FlexibleNamingSymbol("b"));
}
public void testInteger() {
parser.parse("$x = -21");
verifyVariable("x", new Long(-21));
}
public void testDouble() {
parser.parse("$x = -21.0");
verifyVariable("x", new Double(-21.0));
}
public void testList() {
parser.parse("$x = [\"a\", \"b\", \"c\"]");
verifyVariable("x", AUtil.list(new String[]{"a","b","c"}));
}
public void testSet() {
parser.parse("$x = {\"a\", \"b\", \"c\"}");
verifyVariable("x", AUtil.set(new String[]{"a","b","c"}));
}
public void testTree() {
parser.parse("$t = t1[t11[],t12[]]");
verifyVariable("t", new Tree("t1", new Tree[]{new Tree("t11"), new Tree("t12")}));
}
public void testTree2() {
parser.parse("$t = t1[t11,t12]");
verifyVariable("t", new Tree("t1", new Tree[]{new Tree("t11"), new Tree("t12")}));
}
public void testBTree() {
parser.parse("$t = t1[t11[;];t12[;]]");
verifyVariable("t", new BinaryTree("t1", new BinaryTree("t11"), new BinaryTree("t12")));
}
public void testBTree2() {
parser.parse("$t = t1[t11;t12]");
verifyVariable("t", new BinaryTree("t1", new BinaryTree("t11"), new BinaryTree("t12")));
}
public void testBTree3() {
parser.parse("$t = t1[t11[null;null];t12[null;null]]");
verifyVariable("t", new BinaryTree("t1", new BinaryTree("t11"), new BinaryTree("t12")));
}
public void testConcatenationPattern() {
parser.parse("$p = /abc/");
verifyVariable(
"p",
new ConcatenationPattern(
new SymbolPattern(new CharSymbol("a")),
new ConcatenationPattern(
new SymbolPattern(new CharSymbol("b")),
new SymbolPattern(new CharSymbol("c")))));
}
public void testUnionPattern() {
parser.parse("$p = /a|b/");
verifyVariable(
"p",
new UnionPattern(
new SymbolPattern(new CharSymbol("a")),
new SymbolPattern(new CharSymbol("b"))));
}
public void testIterationPattern() {
parser.parse("$p = /ab*/");
verifyVariable(
"p",
new ConcatenationPattern(
new SymbolPattern(new CharSymbol("a")),
new IterationPattern(
new SymbolPattern(new CharSymbol("b")), true)));
}
public void testIterationPattern2() {
parser.parse("$p = /ab+/");
verifyVariable(
"p",
new ConcatenationPattern(
new SymbolPattern(new CharSymbol("a")),
new IterationPattern(
new SymbolPattern(new CharSymbol("b")), false)));
}
public void testIntersectionPattern1() {
parser.parse("$p = /(a|A)(b|B)&ab/");
IPattern a = new SymbolPattern(new CharSymbol("a"));
IPattern b = new SymbolPattern(new CharSymbol("b"));
IPattern A = new SymbolPattern(new CharSymbol("A"));
IPattern B = new SymbolPattern(new CharSymbol("B"));
IPattern aA = new VariableBindingPattern(new UnionPattern(a, A), new Variable("1"));
IPattern bB = new VariableBindingPattern(new UnionPattern(b, B), new Variable("2"));
IPattern aAbB = new ConcatenationPattern(aA, bB);
IPattern ab = new ConcatenationPattern(a, b);
IPattern expected = new IntersectionPattern(aAbB, ab);
verifyVariable("p", expected);
}
public void testSubtractionPattern1() {
parser.parse("$p = /(a|A)(b|B)-ab/");
IPattern a = new SymbolPattern(new CharSymbol("a"));
IPattern b = new SymbolPattern(new CharSymbol("b"));
IPattern A = new SymbolPattern(new CharSymbol("A"));
IPattern B = new SymbolPattern(new CharSymbol("B"));
IPattern aA = new VariableBindingPattern(new UnionPattern(a, A), new Variable("1"));
IPattern bB = new VariableBindingPattern(new UnionPattern(b, B), new Variable("2"));
IPattern aAbB = new ConcatenationPattern(aA, bB);
IPattern ab = new ConcatenationPattern(a, b);
IPattern expected = new IntersectionPattern(aAbB, new ComplementPattern(ab));
verifyVariable("p", expected);
}
public void testEmptyPattern1() {
parser.parse("$p = /ab?/");
verifyVariable(
"p",
new ConcatenationPattern(
new SymbolPattern(new CharSymbol("a")),
new UnionPattern(
new SymbolPattern(new CharSymbol("b")),
new EmptyPattern())));
}
public void testEmptyPattern2() {
parser.parse("$p = /()/");
verifyVariable("p", new VariableBindingPattern(new EmptyPattern(), new Variable("1")));
}
public void testAnyCharPattern1() {
parser.parse("$p = /./");
verifyVariable("p", new SymbolPattern(new CharPatternSymbol("\\.")));
}
public void testAnyCharPattern2() {
parser.parse("$p = /\\./");
verifyVariable("p", new SymbolPattern(new CharSymbol(".")));
}
public void testSpecialWord() {
parser.parse("$p = /\\(\\)/");
verifyVariable(
"p",
new ConcatenationPattern(
new SymbolPattern(new CharSymbol("(")),
new SymbolPattern(new CharSymbol(")"))));
}
public void testSpecialWord2() {
parser.parse("$p = /\\a/");
verifyVariable("p", new SymbolPattern(new CharPatternSymbol("\\a")));
}
public void testSpecialWord3() {
parser.parse("$p = /\\\\\\a/");
verifyVariable(
"p",
new ConcatenationPattern(
new SymbolPattern(new CharSymbol("\\")),
new SymbolPattern(new CharPatternSymbol("\\a"))
)
);
}
public void testSpecialWord4() {
parser.parse("$p = /\\\\a/");
verifyVariable(
"p",
new ConcatenationPattern(
new SymbolPattern(new CharSymbol("\\")),
new SymbolPattern(new CharSymbol("a"))
)
);
}
public void testSpecialWord5() {
parser.parse("$p = /a\\//");
verifyVariable(
"p",
new ConcatenationPattern(
new SymbolPattern(new CharSymbol("a")),
new SymbolPattern(new CharSymbol("/"))
)
);
}
public void testSpecialWord6() {
parser.parse("$p = /a\\&/");
verifyVariable(
"p",
new ConcatenationPattern(
new SymbolPattern(new CharSymbol("a")),
new SymbolPattern(new CharSymbol("&"))
)
);
}
public void testComplexPattern() {
parser.parse("$p = /a(b(c))/");
IPattern expect = new SymbolPattern(new CharSymbol("c"));
expect = new VariableBindingPattern(expect, new Variable("2"));
expect = new ConcatenationPattern(new SymbolPattern(new CharSymbol("b")), expect);
expect = new VariableBindingPattern(expect, new Variable("1"));
expect = new ConcatenationPattern(new SymbolPattern(new CharSymbol("a")), expect);
verifyVariable("p", expect);
}
public void testComplexPattern2() {
parser.parse("$p = /a(b)(c)/");
IPattern a = new SymbolPattern(new CharSymbol("a"));
IPattern b = new VariableBindingPattern(new SymbolPattern(new CharSymbol("b")), new Variable("1"));
IPattern c = new VariableBindingPattern(new SymbolPattern(new CharSymbol("c")), new Variable("2"));
IPattern expect = new ConcatenationPattern(a, new ConcatenationPattern(b, c));
verifyVariable("p", expect);
}
public void testComplexPattern3() {
parser.parse("$p = /a(bc|d)/");
verifyVariable(
"p",
new ConcatenationPattern(
new SymbolPattern(new CharSymbol("a")),
new VariableBindingPattern(
new UnionPattern(
new ConcatenationPattern(new SymbolPattern(new CharSymbol("b")), new SymbolPattern(new CharSymbol("c"))),
new SymbolPattern(new CharSymbol("d"))),
new Variable("1"))));
}
public void testComplexPattern4() {
parser.parse("$p = /a*b+/");
verifyVariable(
"p",
new ConcatenationPattern(
new IterationPattern(new SymbolPattern(new CharSymbol("a")), true),
new IterationPattern(new SymbolPattern(new CharSymbol("b")), false)));
}
public void testComplementPattern1() {
parser.parse("$p = /~ab/");
verifyVariable(
"p",
new ConcatenationPattern(
new ComplementPattern(new SymbolPattern(new CharSymbol("a"))),
new SymbolPattern(new CharSymbol("b"))));
}
public void testComplementPattern2() {
parser.parse("$p = /~(ab)/");
verifyVariable(
"p",
new ComplementPattern(
new VariableBindingPattern(
new ConcatenationPattern(
new SymbolPattern(new CharSymbol("a")),
new SymbolPattern(new CharSymbol("b"))),
new Variable("1"))));
}
public void testEmptyTreePattern() {
parser.parse("$p = %{()}");
verifyVariable("p", new EmptyPattern());
}
public void testVariableBindingTreePattern() {
parser.parse("$p = %{a as $x}");
verifyVariable("p", new VariableBindingPattern(new SymbolPattern("a"), new Variable("x")));
}
public void testVariableReferenceTreePattern() {
parser.parse("$p = %{ref($x)}");
verifyVariable("p", new VariableReferencePattern(new Variable("x")));
}
public void testTreeGrammar1() {
parser.parse("$t1 = a[$$t2;$$t3];");
parser.parse("$t2 = a[$$t2;$$t3];");
parser.parse(
"TreeGrammar $$t1 {" +
" $$t1 -> $t1;" +
" $$t2 -> $t2;" +
" $$t3 -> null;" +
"}");
Map m = parser.getResult();
verifyVariable("_",
new TreeGrammar(
new BinaryTreeVariable("t1"),
new ProductionRule[]{
new ProductionRule(new BinaryTreeVariable("t1"), (IBinaryTree) m.get(new Variable("t1"))),
new ProductionRule(new BinaryTreeVariable("t2"), (IBinaryTree) m.get(new Variable("t2"))),
new ProductionRule(new BinaryTreeVariable("t3"), BinaryTree.LEAF),
}
));
}
public void testLastExpression1() {
parser.parse("abc");
verifyVariable("_", new StringSymbol("abc"));
}
public void testLastExpression2() {
parser.parse("/a/");
verifyVariable("_", new SymbolPattern(new CharSymbol("a")));
}
public void testAutomaton() {
parser.parse(
"$fst = " +
"Automaton(<s1>, {<s3>}, {" +
" Transition(<s1>, <s2>, CharSymbol(\"a\"))," +
" Transition(<s2>, <s3>, CharSymbol(\"b\"))," +
"})");
IAutomaton fst = (IAutomaton) parser.getResult().get(new Variable("fst"));
assertTrue(fst != null);
assertTrue(fst.accept(StringSymbol.toCharSymbols("ab")));
}
public void testCharSetPattern1() {
parser.parse("$p = /[ab]/");
verifyVariable(
"p",
new UnionPattern(
new SymbolPattern(new CharSymbol("a")),
new SymbolPattern(new CharSymbol("b"))));
}
public void testCharSetPattern2() {
parser.parse("$p = /[a-c]/");
verifyVariable(
"p",
new UnionPattern(
new UnionPattern(
new SymbolPattern(new CharSymbol("a")),
new SymbolPattern(new CharSymbol("b"))),
new SymbolPattern(new CharSymbol("c"))));
}
public void testCharSetPattern3() {
parser.parse("$p = /[a-cA-C]/");
verifyVariable(
"p",
new UnionPattern(
new UnionPattern(
new UnionPattern(
new SymbolPattern(new CharSymbol("a")),
new SymbolPattern(new CharSymbol("b"))
),
new SymbolPattern(new CharSymbol("c"))
),
new UnionPattern(
new UnionPattern(
new SymbolPattern(new CharSymbol("A")),
new SymbolPattern(new CharSymbol("B"))
),
new SymbolPattern(new CharSymbol("C"))
)
)
);
}
public void testCharSetPattern4() {
parser.parse("$p = /[-]/");
verifyVariable(
"p",
new SymbolPattern(new CharSymbol("-")));
}
public void testCharSetPattern5() {
parser.parse("$p = /[]-]/");
verifyVariable(
"p",
new UnionPattern(
new SymbolPattern(new CharSymbol("]")),
new SymbolPattern(new CharSymbol("-"))));
}
public void testCharSetPattern6() {
parser.parse("$p = /[--.]/");
verifyVariable(
"p",
new UnionPattern(
new SymbolPattern(new CharSymbol("-")),
new SymbolPattern(new CharSymbol("."))));
}
public void testComplementCharSetPattern1() {
parser.parse("$p = /[^ab]/");
verifyVariable(
"p",
new IntersectionPattern( new SymbolPattern(new CharPatternSymbol("\\.")),
new ComplementPattern(
new UnionPattern(
new SymbolPattern(new CharSymbol("a")),
new SymbolPattern(new CharSymbol("b"))
))));
}
public void testComplementCharSetPattern2() {
parser.parse("$p = /[^a-c]/");
verifyVariable(
"p",
new IntersectionPattern( new SymbolPattern(new CharPatternSymbol("\\.")),
new ComplementPattern(
new UnionPattern(
new UnionPattern(
new SymbolPattern(new CharSymbol("a")),
new SymbolPattern(new CharSymbol("b"))),
new SymbolPattern(new CharSymbol("c"))
))));
}
public void testComplementCharSetPattern3() {
parser.parse("$p = /[^a-cA-C]/");
verifyVariable(
"p",
new IntersectionPattern( new SymbolPattern(new CharPatternSymbol("\\.")),
new ComplementPattern(
new UnionPattern(
new UnionPattern(
new UnionPattern(
new SymbolPattern(new CharSymbol("a")),
new SymbolPattern(new CharSymbol("b"))),
new SymbolPattern(new CharSymbol("c"))
),
new UnionPattern(
new UnionPattern(
new SymbolPattern(new CharSymbol("A")),
new SymbolPattern(new CharSymbol("B"))),
new SymbolPattern(new CharSymbol("C"))
))
))
);
}
public void testComplementCharSetPattern4() {
parser.parse("$p = /[^-]/");
verifyVariable(
"p",
new IntersectionPattern( new SymbolPattern(new CharPatternSymbol("\\.")),
new ComplementPattern(new SymbolPattern(new CharSymbol("-")))));
}
public void testComplementCharSetPattern5() {
parser.parse("$p = /[^]-]/");
verifyVariable(
"p",
new IntersectionPattern( new SymbolPattern(new CharPatternSymbol("\\.")),
new ComplementPattern(
new UnionPattern(
new SymbolPattern(new CharSymbol("]")),
new SymbolPattern(new CharSymbol("-"))
))));
}
public void testComplementCharSetPattern6() {
parser.parse("$p = /[^--.]/");
verifyVariable(
"p",
new IntersectionPattern( new SymbolPattern(new CharPatternSymbol("\\.")),
new ComplementPattern(
new UnionPattern(
new SymbolPattern(new CharSymbol("-")),
new SymbolPattern(new CharSymbol("."))))));
}
public void testComplementCharSetPattern7() {
parser.parse("$p = /[^^]/");
verifyVariable(
"p",
new IntersectionPattern( new SymbolPattern(new CharPatternSymbol("\\.")),
new ComplementPattern(new SymbolPattern(new CharSymbol("^")))));
}
}

View File

@ -0,0 +1,185 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.regex.string;
import com.ibm.wala.automaton.regex.string.ComplementPattern;
import com.ibm.wala.automaton.regex.string.ConcatenationPattern;
import com.ibm.wala.automaton.regex.string.EmptyPattern;
import com.ibm.wala.automaton.regex.string.IPattern;
import com.ibm.wala.automaton.regex.string.IntersectionPattern;
import com.ibm.wala.automaton.regex.string.IterationPattern;
import com.ibm.wala.automaton.regex.string.SymbolPattern;
import com.ibm.wala.automaton.regex.string.UnionPattern;
import com.ibm.wala.automaton.regex.string.VariableBindingPattern;
import com.ibm.wala.automaton.regex.string.VariableReferencePattern;
import com.ibm.wala.automaton.string.Symbol;
import com.ibm.wala.automaton.string.Variable;
import junit.framework.TestCase;
public class TestPattern extends TestCase {
public void testEmptyPatternEquality() {
EmptyPattern empty1 = new EmptyPattern();
EmptyPattern empty2 = new EmptyPattern();
assertTrue(empty1.equals(empty2));
assertTrue(empty2.equals(empty1));
}
public void testSymbolPatternEquality() {
IPattern p1 = new SymbolPattern(new Symbol("a"));
IPattern p2 = new SymbolPattern(new Symbol("a"));
assertTrue(p1.equals(p2));
assertTrue(p2.equals(p1));
}
public void testSymbolPatternEquality2() {
IPattern p1 = new SymbolPattern(new Symbol("a"));
IPattern p2 = new SymbolPattern(new Symbol("b"));
assertFalse(p1.equals(p2));
assertFalse(p2.equals(p1));
}
public void testIterationPatternEquality() {
IterationPattern p1 = new IterationPattern(new EmptyPattern(), true);
IterationPattern p2 = new IterationPattern(new EmptyPattern(), true);
assertTrue(p1.equals(p2));
assertTrue(p2.equals(p1));
}
public void testIterationPatternEquality2() {
IterationPattern p1 = new IterationPattern(new SymbolPattern(new Symbol("a")));
IterationPattern p2 = new IterationPattern(new SymbolPattern(new Symbol("b")));
assertFalse(p1.equals(p2));
assertFalse(p2.equals(p1));
}
public void testIterationPatternEquality3() {
IterationPattern p1 = new IterationPattern(new SymbolPattern(new Symbol("a")), true);
IterationPattern p2 = new IterationPattern(new SymbolPattern(new Symbol("a")), false);
assertFalse(p1.equals(p2));
assertFalse(p2.equals(p1));
}
public void testComplementPatternEquality() {
ComplementPattern p1 = new ComplementPattern(new SymbolPattern(new Symbol("a")));
ComplementPattern p2 = new ComplementPattern(new SymbolPattern(new Symbol("a")));
assertTrue(p1.equals(p2));
assertTrue(p2.equals(p1));
}
public void testComplementPatternEquality2() {
ComplementPattern p1 = new ComplementPattern(new SymbolPattern(new Symbol("a")));
ComplementPattern p2 = new ComplementPattern(new SymbolPattern(new Symbol("b")));
assertFalse(p1.equals(p2));
assertFalse(p2.equals(p1));
}
public void testConcatenationPatternEquality() {
ConcatenationPattern p1 = new ConcatenationPattern(
new SymbolPattern(new Symbol("a")),
new EmptyPattern());
ConcatenationPattern p2 = new ConcatenationPattern(
new SymbolPattern(new Symbol("a")),
new EmptyPattern());
assertTrue(p1.equals(p2));
assertTrue(p2.equals(p1));
}
public void testConcatenationPatternEquality2() {
ConcatenationPattern p1 = new ConcatenationPattern(
new SymbolPattern(new Symbol("a")),
new EmptyPattern());
ConcatenationPattern p2 = new ConcatenationPattern(
new SymbolPattern(new Symbol("b")),
new EmptyPattern());
assertFalse(p1.equals(p2));
assertFalse(p2.equals(p1));
}
public void testUnionPatternEquality() {
UnionPattern p1 = new UnionPattern(
new SymbolPattern(new Symbol("a")),
new EmptyPattern());
UnionPattern p2 = new UnionPattern(
new SymbolPattern(new Symbol("a")),
new EmptyPattern());
assertTrue(p1.equals(p2));
assertTrue(p2.equals(p1));
}
public void testUnionPatternEquality2() {
UnionPattern p1 = new UnionPattern(
new SymbolPattern(new Symbol("a")),
new EmptyPattern());
UnionPattern p2 = new UnionPattern(
new SymbolPattern(new Symbol("b")),
new EmptyPattern());
assertFalse(p1.equals(p2));
assertFalse(p2.equals(p1));
}
public void testVariableBindingPatternEquality() {
VariableBindingPattern p1 = new VariableBindingPattern(new SymbolPattern(new Symbol("a")), new Variable("v"));
VariableBindingPattern p2 = new VariableBindingPattern(new SymbolPattern(new Symbol("a")), new Variable("v"));
assertTrue(p1.equals(p2));
assertTrue(p2.equals(p1));
}
public void testVariableBindingPatternEquality2() {
VariableBindingPattern p1 = new VariableBindingPattern(new SymbolPattern(new Symbol("a")), new Variable("v"));
VariableBindingPattern p2 = new VariableBindingPattern(new SymbolPattern(new Symbol("a")), new Variable("w"));
assertFalse(p1.equals(p2));
assertFalse(p2.equals(p1));
}
public void testVariableBindingPatternEquality3() {
VariableBindingPattern p1 = new VariableBindingPattern(new SymbolPattern(new Symbol("a")), new Variable("v"));
VariableBindingPattern p2 = new VariableBindingPattern(new SymbolPattern(new Symbol("b")), new Variable("v"));
assertFalse(p1.equals(p2));
assertFalse(p2.equals(p1));
}
public void testVariableReferencePatternEquality() {
VariableReferencePattern p1 = new VariableReferencePattern(new Variable("v"));
VariableReferencePattern p2 = new VariableReferencePattern(new Variable("v"));
assertTrue(p1.equals(p2));
assertTrue(p2.equals(p1));
}
public void testVariableReferencePatternEquality2() {
VariableReferencePattern p1 = new VariableReferencePattern(new Variable("v"));
VariableReferencePattern p2 = new VariableReferencePattern(new Variable("w"));
assertFalse(p1.equals(p2));
assertFalse(p2.equals(p1));
}
public void testIntersectionPatternEquality() {
IntersectionPattern p1 = new IntersectionPattern(
new SymbolPattern(new Symbol("a")),
new SymbolPattern(new Symbol("a")));
IntersectionPattern p2 = new IntersectionPattern(
new SymbolPattern(new Symbol("a")),
new SymbolPattern(new Symbol("a")));
assertTrue(p1.equals(p2));
assertTrue(p2.equals(p1));
}
public void testIntersectionPatternEquality2() {
IntersectionPattern p1 = new IntersectionPattern(
new SymbolPattern(new Symbol("a")),
new SymbolPattern(new Symbol("a")));
IntersectionPattern p2 = new IntersectionPattern(
new SymbolPattern(new Symbol("a")),
new SymbolPattern(new Symbol("b")));
assertFalse(p1.equals(p2));
assertFalse(p2.equals(p1));
}
}

View File

@ -0,0 +1,103 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.regex.string;
import com.ibm.wala.automaton.parser.*;
import com.ibm.wala.automaton.regex.string.IPattern;
import com.ibm.wala.automaton.regex.string.StringPatternCompiler;
import com.ibm.wala.automaton.string.*;
import com.ibm.wala.automaton.test.AutomatonJunitBase;
public class TestPatternCompiler extends AutomatonJunitBase {
private AmtParser parser;
private StringPatternCompiler stringPatternCompiler;
public void setUp() {
parser = new AmtParser();
stringPatternCompiler = new StringPatternCompiler();
}
public void testCompileAnyChar1() {
IPattern pat = (IPattern) parser.parse("/./").get(new Variable("_"));
IAutomaton a = stringPatternCompiler.compile(pat);
assertTrue(a.accept(StringSymbol.toCharSymbols("\n")));
assertTrue(a.accept(StringSymbol.toCharSymbols("b")));
assertFalse(a.accept(StringSymbol.toCharSymbols("ab")));
}
public void testCompileConcatenation1() {
IPattern pat = (IPattern) parser.parse("/abc/").get(new Variable("_"));
IAutomaton a = stringPatternCompiler.compile(pat);
assertTrue(a.accept(StringSymbol.toCharSymbols("abc")));
assertFalse(a.accept(StringSymbol.toCharSymbols("abcd")));
assertFalse(a.accept(StringSymbol.toCharSymbols("ab")));
}
public void testCompileUnion1() {
IPattern pat = (IPattern) parser.parse("/(ab)|(bc)/").get(new Variable("_"));
IAutomaton a = stringPatternCompiler.compile(pat);
assertTrue(a.accept(StringSymbol.toCharSymbols("ab")));
assertTrue(a.accept(StringSymbol.toCharSymbols("bc")));
assertFalse(a.accept(StringSymbol.toCharSymbols("abc")));
assertFalse(a.accept(StringSymbol.toCharSymbols("a")));
assertFalse(a.accept(StringSymbol.toCharSymbols("bcd")));
}
public void testCompileUnion2() {
IPattern pat = (IPattern) parser.parse("/ab?/").get(new Variable("_"));
IAutomaton a = stringPatternCompiler.compile(pat);
assertTrue(a.accept(StringSymbol.toCharSymbols("ab")));
assertTrue(a.accept(StringSymbol.toCharSymbols("a")));
assertFalse(a.accept(StringSymbol.toCharSymbols("")));
assertFalse(a.accept(StringSymbol.toCharSymbols("abc")));
}
public void testCompileIteration1() {
IPattern pat = (IPattern) parser.parse("/(ab)+/").get(new Variable("_"));
IAutomaton a = stringPatternCompiler.compile(pat);
assertTrue(a.accept(StringSymbol.toCharSymbols("ab")));
assertTrue(a.accept(StringSymbol.toCharSymbols("abab")));
assertFalse(a.accept(StringSymbol.toCharSymbols("aba")));
assertFalse(a.accept(StringSymbol.toCharSymbols("abb")));
assertFalse(a.accept(StringSymbol.toCharSymbols("")));
}
public void testCompileComplement1() {
IPattern pat = (IPattern) parser.parse("/~(ab)/").get(new Variable("_"));
IAutomaton a = stringPatternCompiler.compile(pat);
assertFalse(a.accept(StringSymbol.toCharSymbols("ab")));
assertTrue(a.accept(StringSymbol.toCharSymbols("abab")));
assertTrue(a.accept(StringSymbol.toCharSymbols("aba")));
assertTrue(a.accept(StringSymbol.toCharSymbols("abb")));
assertTrue(a.accept(StringSymbol.toCharSymbols("")));
}
public void testCompileComplement2() {
IPattern pat = (IPattern) parser.parse("/(~a)*/").get(new Variable("_"));
IAutomaton a = stringPatternCompiler.compile(pat);
assertFalse(a.accept(StringSymbol.toCharSymbols("a")));
assertTrue(a.accept(StringSymbol.toCharSymbols("b")));
assertTrue(a.accept(StringSymbol.toCharSymbols("")));
assertTrue(a.accept(StringSymbol.toCharSymbols("aa")));
assertTrue(a.accept(StringSymbol.toCharSymbols("ab")));
assertTrue(a.accept(StringSymbol.toCharSymbols("bb")));
}
public void testCompileComplement3() {
IPattern pat = (IPattern) parser.parse("/[^a]*/").get(new Variable("_"));
IAutomaton a = stringPatternCompiler.compile(pat);
assertFalse(a.accept(StringSymbol.toCharSymbols("a")));
assertTrue(a.accept(StringSymbol.toCharSymbols("b")));
assertTrue(a.accept(StringSymbol.toCharSymbols("")));
assertFalse(a.accept(StringSymbol.toCharSymbols("aa")));
assertFalse(a.accept(StringSymbol.toCharSymbols("ab")));
assertTrue(a.accept(StringSymbol.toCharSymbols("bb")));
}
}

View File

@ -0,0 +1,68 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.regex.string;
import com.ibm.wala.automaton.grammar.string.*;
import com.ibm.wala.automaton.regex.string.StringPatternSymbol;
import com.ibm.wala.automaton.string.*;
import com.ibm.wala.automaton.test.AutomatonJunitBase;
public class TestPatternSymbol extends AutomatonJunitBase {
public void testStringPatternSymbol1() {
StringPatternSymbol pat = new StringPatternSymbol("abc|ABC");
StringSymbol str1 = new StringSymbol("abc");
StringSymbol str2 = new StringSymbol("ABC");
StringSymbol str3 = new StringSymbol("aBC");
MatchContext ctx = new MatchContext();
assertTrue(pat.matches(str1, ctx));
assertEquals(str1, ctx.get(pat));
assertTrue(pat.matches(str2, ctx));
assertEquals(str2, ctx.get(pat));
assertFalse(pat.matches(str3, ctx));
}
public void testStringPatternSymbol2() {
ContextFreeGrammar cfg1 = new ContextFreeGrammar(
new Variable("A"),
new ProductionRule[]{
new ProductionRule(new Variable("A"), StringSymbol.toCharSymbols("abc")),
new ProductionRule(new Variable("A"), StringSymbol.toCharSymbols("ABC")),
}
);
CFGSymbol cs1 = new CFGSymbol(cfg1);
ContextFreeGrammar cfg2 = new ContextFreeGrammar(
new Variable("A"),
new ProductionRule[]{
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("a"), new Variable("B")}),
new ProductionRule(new Variable("A"), new ISymbol[]{new Symbol("A"), new Variable("B")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("b"), new Variable("C")}),
new ProductionRule(new Variable("B"), new ISymbol[]{new Symbol("B"), new Variable("C")}),
new ProductionRule(new Variable("C"), new ISymbol[]{new Symbol("c")}),
new ProductionRule(new Variable("C"), new ISymbol[]{new Symbol("C")}),
}
);
CFGSymbol cs2 = new CFGSymbol(cfg2);
StringPatternSymbol ps = new StringPatternSymbol("abc|ABC");
IMatchContext ctx = new MatchContext();
assertTrue(ps.matches(cs1, ctx));
assertFalse(cs1.matches(ps, ctx));
assertFalse(ps.matches(cs2, ctx));
assertFalse(cs2.matches(ps, ctx));
}
}

View File

@ -0,0 +1,257 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.string;
import java.util.*;
import com.ibm.wala.automaton.*;
import com.ibm.wala.automaton.string.*;
import junit.framework.TestCase;
public class TestAutomaton extends TestCase {
public void testCreate(){
Automaton fst1 = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("A")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("B")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
}
);
assertEquals(new State("s1"), fst1.getInitialState());
assertEquals(1, fst1.getFinalStates().size());
assertEquals(3, fst1.getTransitions().size());
}
public void testEquality(){
Automaton fst1 = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("A")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("B")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
}
);
Automaton fst2 = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("A")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("B")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
}
);
assertEquals(fst1, fst2);
}
public void testGetTransitions() {
Automaton fst1 = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Variable("a"), new Symbol[]{new Symbol("A")}),
new Transition(new State("s2"), new State("s3"), new Variable("b"), new Symbol[]{new Symbol("B")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
new Transition(new State("s2"), new State("s2"), new Symbol("1"), new Symbol[]{new Symbol("1")}),
}
);
assertEquals(3, fst1.getTransitions(new State("s2")).size());
assertEquals(1, fst1.getTransitions(new State("s2"), new Variable("b")).size());
assertEquals(1, fst1.getTransitions(new State("s2"), new Symbol("0")).size());
assertNotNull(fst1.getTransitions(new State("s3")));
assertTrue(fst1.getTransitions(new State("s3")).isEmpty());
}
public void testGetStates() {
Automaton fst1 = new Automaton(
new State("s1"),
new State[]{new State("s1")},
new Transition[]{}
);
Set states = AUtil.set(new State[]{new State("s1")});
assertEquals(states, fst1.getStates());
assertTrue(fst1.getTransitions().isEmpty());
assertNotNull(fst1.getTransitions(new State("s1")));
assertTrue(fst1.getTransitions(new State("s1")).isEmpty());
}
public void testGetTransitionsModified() {
Automaton fst1 = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Variable("a"), new Symbol[]{new Symbol("A")}),
new Transition(new State("s2"), new State("s3"), new Variable("b"), new Symbol[]{new Symbol("B")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
new Transition(new State("s2"), new State("s2"), new Symbol("1"), new Symbol[]{new Symbol("1")}),
}
);
Set expected = AUtil.set(new Transition[]{
new Transition(new State("s1"), new State("s2"), new Variable("a"), new Symbol[]{new Symbol("A")}),
new Transition(new State("s2"), new State("s3"), new Variable("b"), new Symbol[]{new Symbol("B")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
new Transition(new State("s2"), new State("s2"), new Symbol("1"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s3"), new State("s4"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
});
fst1.getTransitions().add(new Transition(new State("s3"), new State("s4"), new Symbol("0"), new Symbol[]{new Symbol("0")}));
assertEquals(expected, fst1.getTransitions());
}
public void testGetAcceptTransitions() {
Automaton fst1 = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Variable("a"), new Symbol[]{new Symbol("A")}),
new Transition(new State("s2"), new State("s3"), new Variable("b"), new Symbol[]{new Symbol("B")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
new Transition(new State("s2"), new State("s2"), new Symbol("1"), new Symbol[]{new Symbol("1")}),
}
);
assertEquals(3, fst1.getTransitions(new State("s2")).size());
assertEquals(0, fst1.getAcceptTransitions(new State("s2"), new Variable("b")).size());
assertEquals(2, fst1.getAcceptTransitions(new State("s2"), new Symbol("0")).size());
}
public void testTranslate() {
Automaton fst1 = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Variable("a"), new Symbol[]{new Symbol("A")}),
new Transition(new State("s2"), new State("s3"), new Variable("b"), new Symbol[]{new Symbol("B")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("1")}),
new Transition(new State("s2"), new State("s2"), new Symbol("1"), new Symbol[]{new Symbol("0")}),
}
);
List l1 = AUtil.list(new Symbol[]{
new Symbol("a"),
new Symbol("0"),
new Symbol("b"),
});
List l2 = AUtil.list(
new List[]{
AUtil.list(new Symbol[]{
new Symbol("A"),
new Symbol("1"),
new Symbol("B"),
}),
});
assertEquals(l2, fst1.translate(l1));
assertTrue(fst1.accept(l1));
List l3 = AUtil.list(new Symbol[]{
new Symbol("a"),
new Symbol("b"),
});
List l4 = AUtil.list(
new List[]{
AUtil.list(new Symbol[]{
new Symbol("A"),
new Symbol("B"),
}),
});
assertEquals(l4, fst1.translate(l3));
assertTrue(fst1.accept(l3));
List l5 = AUtil.list(new Symbol[]{
new Symbol("a"),
new Symbol("b"),
new Symbol("1"),
});
assertFalse(fst1.accept(l5));
}
public void testAcceptEmpty1() {
Automaton fst1 = new Automaton(
new State("s1"),
new State[]{new State("s2"), new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), Transition.EpsilonSymbol, new Symbol[]{new Symbol("A")}),
new Transition(new State("s2"), new State("s3"), Transition.EpsilonSymbol, new Symbol[]{new Symbol("B")}),
}
);
assertTrue(fst1.accept(AUtil.list(new Symbol[0])));
assertEquals(1, fst1.translate(AUtil.list(new Symbol[0])).size());
assertEquals(
AUtil.list(new Symbol[]{new Symbol("A")}),
fst1.translate(AUtil.list(new Symbol[0])).get(0));
}
public void testAutomatonCopy1() {
Automaton fst1 = new Automaton(
new State("s1"),
new State[]{new State("s2")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("A")}),
}
);
Automaton fst2 = (Automaton) fst1.copy(SimpleSTSCopier.defaultCopier);
assertEquals(fst1, fst2);
assertTrue(System.identityHashCode(fst1.getInitialState())
== System.identityHashCode(fst2.getInitialState()));
assertFalse(System.identityHashCode(fst1.getFinalStates())
== System.identityHashCode(fst2.getFinalStates()));
assertTrue(System.identityHashCode((new ArrayList(fst1.getFinalStates())).get(0))
== System.identityHashCode((new ArrayList(fst2.getFinalStates())).get(0)));
assertTrue(System.identityHashCode((new ArrayList(fst1.getTransitions())).get(0))
== System.identityHashCode((new ArrayList(fst2.getTransitions())).get(0)));
}
public void testAutomatonCopy2() {
Automaton fst1 = new Automaton(
new State("s1"),
new State[]{new State("s2")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("A")}),
}
);
Automaton fst2 = (Automaton) fst1.copy(new DeepSTSCopier(SimpleTransitionCopier.defaultCopier));
assertEquals(fst1, fst2);
assertTrue(System.identityHashCode(fst1.getInitialState())
== System.identityHashCode(fst2.getInitialState()));
assertFalse(System.identityHashCode(fst1.getFinalStates())
== System.identityHashCode(fst2.getFinalStates()));
assertTrue(System.identityHashCode((new ArrayList(fst1.getFinalStates())).get(0))
== System.identityHashCode((new ArrayList(fst2.getFinalStates())).get(0)));
assertFalse(System.identityHashCode((new ArrayList(fst1.getTransitions())).get(0))
== System.identityHashCode((new ArrayList(fst2.getTransitions())).get(0)));
}
public void testAutomatonCopy3() {
Automaton fst1 = new Automaton(
new State("s1"),
new State[]{new State("s2")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("A")}),
}
);
Automaton fst2 = (Automaton) fst1.copy(new DeepSTSCopier(new DeepTransitionCopier()));
assertEquals(fst1, fst2);
assertFalse(System.identityHashCode(fst1.getInitialState())
== System.identityHashCode(fst2.getInitialState()));
assertFalse(System.identityHashCode(fst1.getFinalStates())
== System.identityHashCode(fst2.getFinalStates()));
assertFalse(System.identityHashCode((new ArrayList(fst1.getFinalStates())).get(0))
== System.identityHashCode((new ArrayList(fst2.getFinalStates())).get(0)));
assertFalse(System.identityHashCode((new ArrayList(fst1.getTransitions())).get(0))
== System.identityHashCode((new ArrayList(fst2.getTransitions())).get(0)));
}
}

View File

@ -0,0 +1,377 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.string;
import java.util.*;
import com.ibm.wala.automaton.AUtil;
import com.ibm.wala.automaton.string.*;
import com.ibm.wala.automaton.test.AutomatonJunitBase;
public class TestAutomatons extends AutomatonJunitBase {
public void testUseUniqueStates() {
IAutomaton a1 = new Automaton(
s1,
new State[]{s3},
new Transition[]{
new Transition(s1, s2, a),
new Transition(s2, s3, b),
}
);
Map stateMap = new HashMap();
IAutomaton a2 = Automatons.useUniqueStates(a1, a1, stateMap);
Set states = AUtil.set(new State[]{s4, s5, s6});
assertEquals(states, a2.getStates());
assertTrue(states.contains(a2.getInitialState()));
assertTrue(states.containsAll(a2.getFinalStates()));
}
public void testUseUniqueInputSymbols() {
IAutomaton a1 = new Automaton(
s1,
new State[]{s3},
new Transition[]{
new Transition(s1, s2, a),
new Transition(s2, s3, b),
}
);
Map symMap = new HashMap();
IAutomaton a2 = Automatons.useUniqueInputSymbols(a1, a1, symMap);
Set syms = AUtil.set(new Symbol[]{i1, i2});
assertEquals(syms, Automatons.collectInputSymbols(a2));
assertEquals(2, symMap.size());
assertTrue(symMap.get(a).equals(i1)
|| symMap.get(a).equals(i2));
assertTrue(symMap.get(b).equals(i1)
|| symMap.get(b).equals(i2));
}
public void testCreateConcatenation() {
IAutomaton a1 = new Automaton(
s1,
new State[]{s3},
new Transition[]{
new Transition(s1, s2, a),
new Transition(s2, s3, b),
}
);
IAutomaton a2 = new Automaton(
s1,
new State[]{s3},
new Transition[]{
new Transition(s1, s2, a),
new Transition(s2, s3, b),
}
);
Map stateMap = new HashMap();
IAutomaton a3 = Automatons.createConcatenation(a1, a2, stateMap);
assertEquals(
AUtil.set(new State[]{s1, s2, s3, s4, s5, s6}),
a3.getStates());
assertEquals(5, a3.getTransitions().size());
assertTrue(a3.accept(AUtil.list(new Symbol[]{a, b, a, b})));
assertFalse(a3.accept(AUtil.list(new Symbol[]{a, b, a})));
}
public void testCreateUnion() {
IAutomaton a1 = new Automaton(
s1,
new State[]{s3},
new Transition[]{
new Transition(s1, s2, a),
new Transition(s2, s3, b),
}
);
IAutomaton a2 = new Automaton(
s1,
new State[]{s3},
new Transition[]{
new Transition(s1, s2, A),
new Transition(s2, s3, B),
}
);
Map stateMap = new HashMap();
IAutomaton a3 = Automatons.createUnion(a1, a2, stateMap);
assertEquals(
AUtil.set(new State[]{s1, s2, s3, s4, s5, s6, s7}),
a3.getStates());
assertEquals(6, a3.getTransitions().size());
Automatons.eliminateEpsilonTransitions(a3);
Automatons.eliminateNonDeterministics(a3);
assertTrue(a3.accept(AUtil.list(new Symbol[]{a, b})));
assertTrue(a3.accept(AUtil.list(new Symbol[]{A, B})));
assertFalse(a3.accept(AUtil.list(new Symbol[]{A, b})));
}
public void testCreateComplement() {
IAutomaton a1 = new Automaton(
s1,
new State[]{s3},
new Transition[]{
new Transition(s1, s2, a),
new Transition(s2, s3, b),
}
);
IAutomaton a2 = Automatons.createComplement(a1);
assertFalse(a2.accept(AUtil.list(new Symbol[]{a, b})));
assertTrue(a2.accept(AUtil.list(new Symbol[]{a, B})));
assertTrue(a2.accept(AUtil.list(new Symbol[]{A, B})));
}
public void testCreateIntersection() {
IAutomaton a1 = new Automaton(
s1,
new State[]{s3},
new Transition[]{
new Transition(s1, s2, a),
new Transition(s2, s3, b),
}
);
IAutomaton a2 = new Automaton(
s2,
new State[]{s4},
new Transition[]{
new Transition(s2, s3, a),
new Transition(s3, s4, b),
new Transition(s3, s4, c),
}
);
IAutomaton a3 = Automatons.createIntersection(a1, a2);
assertTrue(a3.accept(AUtil.list(new Symbol[]{a, b})));
assertFalse(a3.accept(AUtil.list(new Symbol[]{a, c})));
IAutomaton a4 = Automatons.createIntersection(a2, a1);
assertTrue(a4.accept(AUtil.list(new Symbol[]{a, b})));
assertFalse(a4.accept(AUtil.list(new Symbol[]{a, c})));
}
public void testCreateSubtraction() {
IAutomaton a1 = new Automaton(
s1,
new State[]{s3},
new Transition[]{
new Transition(s1, s2, a),
new Transition(s2, s3, b),
new Transition(s1, s2, A),
new Transition(s2, s3, B),
}
);
IAutomaton a2 = new Automaton(
s1,
new State[]{s3},
new Transition[]{
new Transition(s1, s2, A),
new Transition(s2, s3, B),
}
);
IAutomaton a3 = Automatons.createSubtraction(a1, a2);
//Automatons.expand(a3, AUtil.set(new ISymbol[]{a, b, c, A, B}));
//System.err.println(Automatons.toGraphviz(a3));
assertTrue(a3.accept(AUtil.list(new Symbol[]{a, b})));
assertTrue(a3.accept(AUtil.list(new Symbol[]{A, b})));
assertTrue(a3.accept(AUtil.list(new Symbol[]{a, B})));
assertFalse(a3.accept(AUtil.list(new Symbol[]{A, B})));
}
public void testEliminateEpsilonTransitions() {
Automaton fst1 = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), Transition.EpsilonSymbol, new Symbol[]{new Symbol("A")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("B")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
}
);
Automaton fst2 = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("A"), new Symbol("B")}),
new Transition(new State("s1"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("A"), new Symbol("0")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("B")}),
new Transition(new State("s2"), new State("s2"), new Symbol("0"), new Symbol[]{new Symbol("0")}),
}
);
Automatons.eliminateEpsilonTransitions(fst1);
assertEquals(fst2, fst1);
}
public void testEliminateEpsilonTransitions2() {
Automaton fst1 = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), Transition.EpsilonSymbol, new Symbol[]{new Symbol("A")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("B")}),
new Transition(new State("s2"), new State("s2"), Transition.EpsilonSymbol, new Symbol[]{new Symbol("0")}),
}
);
Automaton fst2 = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("A"), new Symbol("B")}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{new Symbol("B")}),
}
);
Automatons.eliminateEpsilonTransitions(fst1);
assertEquals(fst2, fst1);
}
public void testEliminateEpsilonTransitions3() {
Automaton fst1 = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("A")}),
new Transition(new State("s2"), new State("s3"), Transition.EpsilonSymbol, new Symbol[]{}),
new Transition(new State("s3"), new State("s4"), new Symbol("b"), new Symbol[]{new Symbol("B")}),
new Transition(new State("s4"), new State("s3"), Transition.EpsilonSymbol, new Symbol[]{}),
}
);
Automaton fst2 = new Automaton(
new State("s1"),
new State[]{new State("s2"), new State("s3"), new State("s4")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{new Symbol("A")}),
new Transition(new State("s2"), new State("s4"), new Symbol("b"), new Symbol[]{new Symbol("B")}),
new Transition(new State("s3"), new State("s4"), new Symbol("b"), new Symbol[]{new Symbol("B")}),
new Transition(new State("s4"), new State("s4"), new Symbol("b"), new Symbol[]{new Symbol("B")}),
}
);
Automatons.eliminateEpsilonTransitions(fst1);
assertEquals(fst2, fst1);
}
public void testEliminateEpsilonTransitions4() {
Automaton fst1 = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), Transition.EpsilonSymbol, new Symbol[]{new CharSymbol("0")}),
new FilteredTransition(new State("s2"), new State("s3"), new Variable("x"), new Symbol[]{new Variable("x")},
new FilteredTransition.IFilter(){
public List invoke(ISymbol symbol, List outputs) {
String s = ((CharSymbol)outputs.get(0)).getName().toUpperCase();
return AUtil.list(new ISymbol[]{new CharSymbol(s)});
}},
new FilteredTransition.ICondition(){
private CharSymbol c = new CharSymbol("c");
public boolean accept(ISymbol symbol, IMatchContext ctx) {
return c.equals(symbol);
}}),
}
);
Automatons.eliminateEpsilonTransitions(fst1);
List r1 = fst1.translate(StringSymbol.toCharSymbols("a"));
assertTrue(r1.isEmpty());
List r2 = fst1.translate(StringSymbol.toCharSymbols("c"));
assertEquals(1, r2.size());
assertEquals(StringSymbol.toCharSymbols("0C"), r2.get(0));
List r3 = fst1.translate(StringSymbol.toCharSymbols("cc"));
assertTrue(r3.isEmpty());
}
public void testEliminateEpsilonTransitions5() {
Automaton fst1 = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new FilteredTransition(new State("s1"), new State("s2"), new Variable("x"), new Symbol[]{new Variable("x")},
new FilteredTransition.IFilter(){
public List invoke(ISymbol symbol, List outputs) {
String s = ((CharSymbol)outputs.get(0)).getName().toUpperCase();
return AUtil.list(new ISymbol[]{new CharSymbol(s)});
}
},
new FilteredTransition.ICondition(){
private CharSymbol c = new CharSymbol("c");
public boolean accept(ISymbol symbol, IMatchContext ctx) {
return c.equals(symbol);
}
}),
new Transition(new State("s2"), new State("s3"), Transition.EpsilonSymbol, new Symbol[]{}),
new Transition(new State("s3"), new State("s1"), Transition.EpsilonSymbol, new Symbol[]{}),
}
);
Automatons.eliminateEpsilonTransitions(fst1);
List r1 = fst1.translate(StringSymbol.toCharSymbols("a"));
assertTrue(r1.isEmpty());
List r2 = fst1.translate(StringSymbol.toCharSymbols("c"));
assertEquals(1, r2.size());
assertEquals(StringSymbol.toCharSymbols("C"), r2.get(0));
// TODO: should fix this.
/*
List r3 = fst1.translate(StringSymbol.toCharSymbols("ccc"));
assertEquals(1, r2.size());
assertEquals(StringSymbol.toCharSymbols("CCC"), r2.get(0));
*/
}
public void testEliminateNonDeterministics() {
Automaton fst1 = new Automaton(
new State("s1"),
new State[]{new State("s3")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{}),
new Transition(new State("s2"), new State("s2"), new Symbol("b"), new Symbol[]{}),
}
);
Automaton fst2 = new Automaton(
new State("s1"),
new State[]{new State("s4")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{}),
new Transition(new State("s2"), new State("s4"), new Symbol("b"), new Symbol[]{}),
new Transition(new State("s4"), new State("s4"), new Symbol("b"), new Symbol[]{}),
}
);
Automatons.eliminateNonDeterministics(fst1);
assertEquals(fst2, fst1);
}
public void testEliminateNonDeterministics2() {
Automaton fst1 = new Automaton(
new State("s1"),
new State[]{new State("s4")},
new Transition[]{
new Transition(new State("s1"), new State("s2"), new Symbol("a"), new Symbol[]{}),
new Transition(new State("s2"), new State("s3"), new Symbol("b"), new Symbol[]{}),
new Transition(new State("s3"), new State("s4"), new Symbol("c"), new Symbol[]{}),
new Transition(new State("s1"), new State("s5"), new Symbol("a"), new Symbol[]{}),
new Transition(new State("s5"), new State("s6"), new Symbol("b"), new Symbol[]{}),
new Transition(new State("s6"), new State("s7"), new Symbol("c"), new Symbol[]{}),
}
);
Automaton fst2 = new Automaton(
new State("s1"),
new State[]{new State("s10")},
new Transition[]{
new Transition(new State("s1"), new State("s8"), new Symbol("a"), new Symbol[]{}),
new Transition(new State("s8"), new State("s9"), new Symbol("b"), new Symbol[]{}),
new Transition(new State("s9"), new State("s10"), new Symbol("c"), new Symbol[]{}),
}
);
Automatons.eliminateNonDeterministics(fst1);
assertEquals(fst2, fst1);
}
}

View File

@ -0,0 +1,42 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.string;
import com.ibm.wala.automaton.string.*;
import junit.framework.TestCase;
public class TestState extends TestCase {
public void testState(){
State s = new State("name");
assertEquals("name", s.getName());
}
public void testStateSetName(){
IState s = new State("name");
s = NameReplaceStateCopier.setName(s, "foo");
assertEquals("foo", s.getName());
}
public void testStateEquality(){
State s1 = new State("name");
State s2 = new State("name");
State s3 = new State("foo");
assertTrue(s1.equals(s2));
assertFalse(s1.equals(s3));
}
public void testStateCopy1() {
IState s1 = new State("s1");
IState s2 = (IState) s1.copy(SimpleStateCopier.defaultCopier);
assertEquals(s1, s2);
}
}

View File

@ -0,0 +1,179 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.string;
import com.ibm.wala.automaton.string.*;
import junit.framework.TestCase;
public class TestSymbol extends TestCase {
public void testSymbolName(){
Symbol s = new Symbol("name");
assertEquals("name", s.getName());
}
/*
public void testSymbolSetName(){
Symbol s = new Symbol("name");
s.setName("foo");
assertEquals("foo", s.getName());
}
*/
public void testSymbolSize(){
Symbol s = new Symbol("name");
assertEquals(0, s.size());
}
public void testSymbolEquality(){
Symbol s1 = new Symbol("name");
Symbol s2 = new Symbol("name");
Symbol s3 = new Symbol("foo");
assertTrue(s1.equals(s2));
assertFalse(s1.equals(s3));
}
public void testVariableMatches(){
Symbol v = new Variable("v");
Symbol s = new Symbol("name");
MatchContext ctx = new MatchContext();
assertTrue(v.matches(s, ctx));
ISymbol r = VariableReplacer.replace(v, ctx);
assertEquals(s, r);
assertTrue(System.identityHashCode(s) == System.identityHashCode(r));
}
public void testVariableMatches2(){
Symbol v = new Variable("v");
Symbol s = new Symbol("name");
MatchContext ctx = new MatchContext();
assertFalse(s.matches(v, ctx));
}
public void testPrefixNew() {
PrefixedSymbol s = new PrefixedSymbol(new Symbol("p"), new Symbol("a"));
assertEquals("p:a", s.getName());
assertEquals("p", s.getPrefix().getName());
assertEquals("a", s.getLocal().getName());
}
/*
public void testPrefixSetName() {
PrefixedSymbol s = new PrefixedSymbol(new StringSymbol("p"), new StringSymbol("a"));
s.setName("q:a");
assertEquals(new PrefixedSymbol(new StringSymbol("q"), new StringSymbol("a")), s);
}
public void testPrefixSetName2() {
PrefixedSymbol s = new PrefixedSymbol(new StringSymbol("p"), new StringSymbol("a"));
s.setName("b");
assertEquals(new PrefixedSymbol(new StringSymbol("p"), new StringSymbol("b")), s);
}
public void testPrefixSetName3() {
PrefixedSymbol s = new PrefixedSymbol(new CharSymbol("p"), new CharSymbol("a"));
s.setName("b");
assertEquals(new PrefixedSymbol(new CharSymbol("p"), new CharSymbol("b")), s);
assertNotSame(new PrefixedSymbol(new CharSymbol("p"), new Symbol("b")), s);
assertNotSame(new PrefixedSymbol(new CharSymbol("p"), new StringSymbol("b")), s);
}
*/
public void testPrefixEquality() {
ISymbol s1 = new PrefixedSymbol(new Symbol("p"), new Symbol("name"));
ISymbol s2 = new PrefixedSymbol(new Symbol("p"), new Symbol("name"));
assertTrue(s1.equals(s2));
}
public void testPrefixEquality2() {
ISymbol s1 = new PrefixedSymbol(new Symbol("p"), new Symbol("name"));
ISymbol s2 = new PrefixedSymbol(new Symbol("q"), new Symbol("name"));
assertFalse(s1.equals(s2));
}
public void testPrefixEquality3() {
ISymbol s1 = new PrefixedSymbol(new Symbol("p"), new Symbol("name"));
ISymbol s2 = new PrefixedSymbol(new Symbol("p"), new Symbol("foo"));
assertFalse(s1.equals(s2));
}
public void testPrefixMatches() {
ISymbol s1 = new PrefixedSymbol(new Variable("x"), new Symbol("name"));
ISymbol s2 = new PrefixedSymbol(new Symbol("p"), new Symbol("name"));
MatchContext ctx = new MatchContext();
assertTrue(s1.matches(s2, ctx));
assertEquals(new Symbol("p"), ctx.get(new Variable("x")));
}
public void testPrefixMatches2() {
ISymbol s1 = new Variable("x");
ISymbol s2 = new PrefixedSymbol(new Symbol("p"), new Symbol("name"));
MatchContext ctx = new MatchContext();
assertTrue(s1.matches(s2, ctx));
assertEquals(s2, ctx.get(new Variable("x")));
}
public void testPrefixMatches3() {
ISymbol s1 = new PrefixedSymbol(new Symbol("x"), new Symbol("name"));
ISymbol s2 = new PrefixedSymbol(new Symbol("p"), new Symbol("foo"));
MatchContext ctx = new MatchContext();
assertFalse(s1.matches(s2, ctx));
}
public void testSimpleSymbolCopier() {
PrefixedSymbol s1 = new PrefixedSymbol(new Symbol("x"), new Symbol("name"));
PrefixedSymbol s2 = (PrefixedSymbol) s1.copy(SimpleSymbolCopier.defaultCopier);
assertEquals(s1, s2);
assertFalse(System.identityHashCode(s1) == System.identityHashCode(s2));
assertTrue(System.identityHashCode(s1.getPrefix()) == System.identityHashCode(s2.getPrefix()));
assertTrue(System.identityHashCode(s1.getLocal()) == System.identityHashCode(s2.getLocal()));
}
public void testDeepSymbolCopier() {
PrefixedSymbol s1 = new PrefixedSymbol(new Symbol("x"), new Symbol("name"));
PrefixedSymbol s2 = (PrefixedSymbol) s1.copy(DeepSymbolCopier.defaultCopier);
assertEquals(s1, s2);
assertFalse(System.identityHashCode(s1) == System.identityHashCode(s2));
assertFalse(System.identityHashCode(s1.getPrefix()) == System.identityHashCode(s2.getPrefix()));
assertFalse(System.identityHashCode(s1.getLocal()) == System.identityHashCode(s2.getLocal()));
}
public void testVariableReplacer1() {
PrefixedSymbol s1 = new PrefixedSymbol(new Variable("x"), new Symbol("name"));
IMatchContext ctx = new MatchContext();
ctx.put(new Variable("x"), new Symbol("p"));
PrefixedSymbol s2 = (PrefixedSymbol) s1.copy(new VariableReplacer(ctx));
PrefixedSymbol expected = new PrefixedSymbol(new Symbol("p"), new Symbol("name"));
assertEquals(expected, s2);
assertFalse(System.identityHashCode(s1) == System.identityHashCode(s2));
assertFalse(System.identityHashCode(s1.getPrefix()) == System.identityHashCode(s2.getPrefix()));
assertFalse(System.identityHashCode(s1.getLocal()) == System.identityHashCode(s2.getLocal()));
}
public void testVariableReplacer2() {
PrefixedSymbol s1 = new PrefixedSymbol(new Variable("x"), new Symbol("name"));
IMatchContext ctx = new MatchContext();
PrefixedSymbol s2 = (PrefixedSymbol) s1.copy(new VariableReplacer(ctx));
assertEquals(s1, s2);
assertFalse(System.identityHashCode(s1) == System.identityHashCode(s2));
assertTrue(System.identityHashCode(s1.getPrefix()) == System.identityHashCode(s2.getPrefix()));
assertFalse(System.identityHashCode(s1.getLocal()) == System.identityHashCode(s2.getLocal()));
}
public void testVariableReplacer3() {
Variable x = new Variable("x");
IMatchContext ctx = new MatchContext();
ctx.put(x, null);
ISymbol result = VariableReplacer.replace(x, ctx);
assertEquals(null, result);
}
}

View File

@ -0,0 +1,182 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.string;
import java.util.*;
import com.ibm.wala.automaton.AUtil;
import com.ibm.wala.automaton.string.*;
import junit.framework.TestCase;
public class TestTransition extends TestCase {
public void testEquality() {
Transition t1 = new Transition(new State("s1"), new State("s2"), new Symbol("a"), new ISymbol[]{new Symbol("b")});
Transition t2 = new Transition(new State("s1"), new State("s2"), new Symbol("a"), new ISymbol[]{new Symbol("b")});
assertEquals(t1, t2);
assertEquals(t2, t1);
}
public void testEquality2() {
Transition t1 = new Transition(new State("s1"), new State("s2"), new Symbol("a"), new ISymbol[]{new Symbol("b")});
Transition t2 = new Transition(new State("s1"), new State("s2"), new Symbol("a"), new ISymbol[]{new Symbol("b"), new Symbol("c")});
assertFalse(t1.equals(t2));
assertFalse(t2.equals(t1));
}
public void testEquality3() {
Transition t1 = new Transition(new State("s1"), new State("s2"), new Symbol("a"), new ISymbol[]{new Symbol("b")});
Transition t2 = new Transition(new State("s1"), new State("s3"), new Symbol("a"), new ISymbol[]{new Symbol("b")});
assertFalse(t1.equals(t2));
assertFalse(t2.equals(t1));
}
public void testEquality4() {
Transition t1 = new Transition(new State("s1"), new State("s2"), new Symbol("a"));
Transition t2 = new Transition(new State("s1"), new State("s2"), new Symbol("a"), new ISymbol[]{});
assertTrue(t1.equals(t2));
assertTrue(t2.equals(t1));
}
public void testTransit(){
Transition t = new Transition(new State("s1"), new State("s2"), new Symbol("a"), new ISymbol[]{new Symbol("b")});
assertTrue(t.accept(new Symbol("a"), new MatchContext()));
List l = new ArrayList();
l.add(new Symbol("b"));
assertEquals(l, t.transit(new Symbol("a")));
}
public void testTransitWithVariable(){
State s1 = new State("s1");
State s2 = new State("s2");
Variable v = new Variable("v");
Symbol a = new Symbol("a");
Transition t = new Transition(s1, s2, v, new ISymbol[]{v});
List l = new ArrayList();
l.add(a);
assertEquals(l, t.transit(a));
}
public void testFilteredTransition1() {
State s1 = new State("s1");
State s2 = new State("s2");
Variable v = new Variable("v");
Symbol a = new Symbol("a");
Symbol A = new Symbol("A");
Transition t = new FilteredTransition(s1, s2, v, new ISymbol[]{v},
new FilteredTransition.IFilter(){
public List invoke(ISymbol symbol, List outputs) {
List result = new ArrayList();
for (Iterator i = outputs.iterator(); i.hasNext(); ) {
ISymbol s = (ISymbol) i.next();
s = NameReplaceSymbolCopier.setName(s, s.getName().toUpperCase());
result.add(s);
}
return result;
}
});
List l = new ArrayList();
l.add(A);
assertEquals(l, t.transit(a));
}
public void testFilteredTransition2() {
State s1 = new State("s1");
State s2 = new State("s2");
Variable v = new Variable("v");
Symbol a = new Symbol("a");
Symbol A = new Symbol("A");
Symbol B = new Symbol("B");
Symbol C = new Symbol("C");
Transition t = new FilteredTransition(s1, s2, v, new ISymbol[]{v},
new FilteredTransition.IFilter(){
public List invoke(ISymbol symbol, List outputs) {
List result = new ArrayList();
for (Iterator i = outputs.iterator(); i.hasNext(); ) {
ISymbol s = (ISymbol) i.next();
s = NameReplaceSymbolCopier.setName(s, s.getName().toUpperCase());
result.add(s);
}
return result;
}
});
t.appendOutputSymbols(AUtil.list(new Object[]{B, C}));
List l = AUtil.list(new Object[]{A, B, C});
assertEquals(l, t.transit(a));
}
public void testFilteredTransition3() {
State s1 = new State("s1");
State s2 = new State("s2");
Variable v = new Variable("v");
Symbol a = new Symbol("a");
Symbol A = new Symbol("A");
Symbol B = new Symbol("B");
Symbol C = new Symbol("C");
Transition t = new FilteredTransition(s1, s2, v, new ISymbol[]{v},
new FilteredTransition.IFilter(){
public List invoke(ISymbol symbol, List outputs) {
List result = new ArrayList();
for (Iterator i = outputs.iterator(); i.hasNext(); ) {
ISymbol s = (ISymbol) i.next();
s = NameReplaceSymbolCopier.setName(s, s.getName().toUpperCase());
result.add(s);
}
return result;
}
});
t.prependOutputSymbols(AUtil.list(new Object[]{C, B}));
List l = AUtil.list(new Object[]{C, B, A});
assertEquals(l, t.transit(a));
}
public void testComplementTransition1() {
State s1 = new State("s1");
State s2 = new State("s2");
Variable v = new Variable("v");
Symbol a = new Symbol("a");
Symbol A = new Symbol("A");
Symbol b = new Symbol("b");
Transition t1 = new Transition(s1, s2, a, new ISymbol[]{A});
Transition t2 = new Transition(s1, s2, A, new ISymbol[]{a});
Transition ct = new ComplementTransition(s1, s2, v, new ISymbol[]{v}, null, new ITransition[]{t1, t2});
assertEquals(A, t1.transit(a).get(0));
assertEquals(a, t2.transit(A).get(0));
assertFalse(ct.accept(a, new MatchContext()));
assertFalse(ct.accept(A, new MatchContext()));
assertTrue(ct.accept(b, new MatchContext()));
assertEquals(b, ct.transit(b).get(0));
}
public void testIntersectionTransition1() {
State s1 = new State("s1");
State s2 = new State("s2");
Variable v = new Variable("v");
Symbol a = new Symbol("a");
Symbol a2 = new Symbol("a");
Symbol b = new Symbol("b");
Transition t1 = new Transition(s1, s2, a, new ISymbol[]{a});
Transition t2 = new Transition(s1, s2, a2, new ISymbol[]{a2});
Transition t3 = new Transition(s1, s2, b, new ISymbol[]{b});
Transition t12 = new IntersectionTransition(s1, s2, v, new ISymbol[]{v}, null, new ITransition[]{t1, t2});
Transition t13 = new IntersectionTransition(s1, s2, v, new ISymbol[]{v}, null, new ITransition[]{t1, t3});
assertEquals(a, t12.transit(a).get(0));
assertFalse(t13.accept(a, new MatchContext()));
}
}

View File

@ -0,0 +1,70 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.tree;
import com.ibm.wala.automaton.string.*;
import com.ibm.wala.automaton.tree.*;
public class TestBinaryTree extends TreeJunitBase {
public void testBTreeEquality(){
BinaryTree t1 = new BinaryTree("a1", new BinaryTree("a11"), new BinaryTree("a12"));
BinaryTree t2 = new BinaryTree("a1", new BinaryTree("a11"), new BinaryTree("a12"));
assertEquals(t1, t2);
}
public void testBTreeEquality2(){
BinaryTree t1 = new BinaryTree("a1", new BinaryTree("a11"), new BinaryTree("a12"));
BinaryTree t2 = new BinaryTree("a1", new BinaryTree("a11"), new BinaryTree("b12"));
assertFalse(t1.equals(t2));
}
public void testBTreeSetLeft(){
BinaryTree t1 = new BinaryTree("a1", new BinaryTree("a11"), new BinaryTree("a12"));
BinaryTree t2 = new BinaryTree("a1", new BinaryTree("b11"), new BinaryTree("a12"));
t2.setLeft(new BinaryTree("a11"));
assertEquals(t1, t2);
}
public void testBTreeSetRight(){
BinaryTree t1 = new BinaryTree("a1", new BinaryTree("a11"), new BinaryTree("a12"));
BinaryTree t2 = new BinaryTree("a1", new BinaryTree("a11"), new BinaryTree("b12"));
t2.setRight(new BinaryTree("a12"));
assertEquals(t1, t2);
}
public void testBTreeSize(){
BinaryTree t = new BinaryTree("a1", new BinaryTree("a11"), new BinaryTree("a12"));
assertEquals(2, t.size());
}
public void testBTreeVariableMatches(){
BinaryTree t1 = new BinaryTree("a1", new BinaryTree("a11"), new BinaryTree("a12"));
BinaryTree t2 = new BinaryTree("a1", new BinaryTree("a11"), new BinaryTreeVariable("v"));
MatchContext ctx = new MatchContext();
boolean result = t2.matches(t1, ctx);
assertTrue(result);
IBinaryTree t3 = (IBinaryTree) ctx.get(new BinaryTreeVariable("v"));
assertEquals(new BinaryTree("a12"), t3);
IBinaryTree t4 = (IBinaryTree) VariableReplacer.replace(t2, ctx);
assertEquals(t1, t4);
}
public void testBTreeVariableMatches2(){
BinaryTree t1 = new BinaryTree("a1", new BinaryTree("a11"), new BinaryTree("a12"));
BinaryTree t2 = new BinaryTree("a1", new BinaryTree("a11"), new BinaryTreeVariable("v"));
MatchContext ctx = new MatchContext();
boolean result = t1.matches(t2, ctx);
assertFalse(result);
}
}

View File

@ -0,0 +1,126 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.tree;
import java.util.*;
import com.ibm.wala.automaton.AUtil;
import com.ibm.wala.automaton.string.*;
import com.ibm.wala.automaton.tree.*;
public class TestBottomUpTreeAutomaton extends TreeJunitBase {
/*
* null -> <s1>(null)
*
* u2[<s1>(x), <s1>(y)] -> <s2>(t2[x,y])
*
* u1[<s1>(x), <s1>(y)] -> <s1>(u1[x, y])
* u1[<s1>(x), <s2>(y)] -> <s3>(t1[x, y])
* u1[<s1>(x), <s3>(y)] -> <s3>(y)
*
* u1[<s2>(x), <s1>(y)] -> <s3>(t1[x, y])
* u1[<s2>(x), <s2>(y)] -> <s3>(t1[x, y])
* u1[<s2>(x), <s3>(y)] -> <s3>(y)
*
* u1[<s3>(x), <s1>(y)] -> <s3>(x)
* u1[<s3>(x), <s2>(y)] -> <s3>(x)
* u1[<s3>(x), <s3>(y)] -> <s3>(x)
* u1[<s3>(x), <s3>(y)] -> <s3>(y)
*/
public void testUndeterministicBottomUpTreeAutomatonTranslate() {
State s1 = new State("s1");
State s2 = new State("s2");
State s3 = new State("s3");
BinaryTreeVariable x = new BinaryTreeVariable("x");
BinaryTreeVariable y = new BinaryTreeVariable("y");
BottomUpTreeAutomaton butt = new BottomUpTreeAutomaton(
new State[]{s3},
new TreeTransition[]{
new TreeTransition(
BinaryTree.LEAF,
new StateBinaryTree(s1, BinaryTree.LEAF)
),
new TreeTransition(
new BinaryTree("u2", new StateBinaryTree(s1, x), new StateBinaryTree(s1, y)),
new StateBinaryTree(s2, new BinaryTree("t2", x, y))
),
new TreeTransition(
new BinaryTree("u1", new StateBinaryTree(s1, x), new StateBinaryTree(s1, y)),
new StateBinaryTree(s1, new BinaryTree("u1", x, y))
),
new TreeTransition(
new BinaryTree("u1", new StateBinaryTree(s1, x), new StateBinaryTree(s2, y)),
new StateBinaryTree(s3, new BinaryTree("t1", x, y))
),
new TreeTransition(
new BinaryTree("u1", new StateBinaryTree(s1, x), new StateBinaryTree(s3, y)),
new StateBinaryTree(s3, y)
),
new TreeTransition(
new BinaryTree("u1", new StateBinaryTree(s2, x), new StateBinaryTree(s1, y)),
new StateBinaryTree(s3, new BinaryTree("t1", x, y))
),
new TreeTransition(
new BinaryTree("u1", new StateBinaryTree(s2, x), new StateBinaryTree(s2, y)),
new StateBinaryTree(s3, new BinaryTree("t1", x, y))
),
new TreeTransition(
new BinaryTree("u1", new StateBinaryTree(s2, x), new StateBinaryTree(s3, y)),
new StateBinaryTree(s3, y)
),
new TreeTransition(
new BinaryTree("u1", new StateBinaryTree(s3, x), new StateBinaryTree(s1, y)),
new StateBinaryTree(s3, x)
),
new TreeTransition(
new BinaryTree("u1", new StateBinaryTree(s3, x), new StateBinaryTree(s2, y)),
new StateBinaryTree(s3, x)
),
new TreeTransition(
new BinaryTree("u1", new StateBinaryTree(s3, x), new StateBinaryTree(s3, y)),
new StateBinaryTree(s3, x)
),
new TreeTransition(
new BinaryTree("u1", new StateBinaryTree(s3, x), new StateBinaryTree(s3, y)),
new StateBinaryTree(s3, y)
),
}
);
BinaryTree t =
new BinaryTree("u1",
new BinaryTree("u1",
new BinaryTree("u2"),
new BinaryTree("u1")),
new BinaryTree("u1"));
assertTrue(butt.accept(t));
BinaryTree ex1 =
new BinaryTree("t1",
new BinaryTree("t2"),
new BinaryTree("u1"));
Set ex = AUtil.set(new BinaryTree[]{ex1});
Set result = butt.translate(t);
assertEquals(ex, result);
BinaryTree tt =
new BinaryTree("u1",
new BinaryTree("u1"),
new BinaryTree("u1"));
assertFalse(butt.accept(tt));
assertFalse(butt.accept(null));
}
}

View File

@ -0,0 +1,73 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.tree;
import java.util.*;
import com.ibm.wala.automaton.AUtil;
import com.ibm.wala.automaton.string.*;
import com.ibm.wala.automaton.tree.*;
public class TestTopDownTreeAutomaton extends TreeJunitBase {
public void testUndeterministicTopDownTreeAutomatonTranslate() {
State s1 = new State("s1");
State s2 = new State("s2");
State s3 = new State("s3");
BinaryTreeVariable x = new BinaryTreeVariable("x");
BinaryTreeVariable y = new BinaryTreeVariable("y");
TopDownTreeAutomaton tdtt = new TopDownTreeAutomaton(
s1,
new TreeTransition[]{
new TreeTransition(
new StateBinaryTree(s1, new BinaryTree("t1", x, y)),
new BinaryTree("u1", new StateBinaryTree(s3, y), new StateBinaryTree(s2, x))
),
new TreeTransition(
new StateBinaryTree(s2, new BinaryTree("t2", x, y)),
new BinaryTree("u2", x, y)
),
new TreeTransition(
new StateBinaryTree(s3, new BinaryTree("t3", x, y)),
new BinaryTree("u3", x, y)
),
new TreeTransition(
new StateBinaryTree(s3, new BinaryTree("t3", x, y)),
new BinaryTree("v3", x, y)
),
}
);
BinaryTree t =
new BinaryTree("t1",
new BinaryTree("t2"),
new BinaryTree("t3"));
assertTrue(tdtt.accept(t));
BinaryTree ex1 =
new BinaryTree("u1",
new BinaryTree("u3"),
new BinaryTree("u2"));
BinaryTree ex2 =
new BinaryTree("u1",
new BinaryTree("v3"),
new BinaryTree("u2"));
Set ex = AUtil.set(new BinaryTree[]{ex1, ex2});
Set result = tdtt.translate(t);
assertEquals(ex, result);
BinaryTree tt =
new BinaryTree("t1",
new BinaryTree("t3"),
new BinaryTree("t3"));
assertFalse(tdtt.accept(tt));
assertFalse(tdtt.accept(null));
}
}

View File

@ -0,0 +1,102 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.tree;
import java.util.*;
import com.ibm.wala.automaton.string.*;
import com.ibm.wala.automaton.tree.*;
public class TestTree extends TreeJunitBase {
public void testTreeEquality(){
Tree a1 = new Tree("a1");
Tree a11 = new Tree("a11");
Tree a12 = new Tree("a12");
a1.addChild(a11);
a1.addChild(a12);
Tree t1 = new Tree("a1", new Tree[]{
new Tree("a11"),
new Tree("a12"),
});
Tree t2 = new Tree("a1", new Tree[]{
new Tree("a11"),
new Tree("b12"),
});
assertEquals(t1, a1);
assertFalse(t2.equals(a1));
}
public void testTreeAddChildren(){
Tree a1 = new Tree("a1");
Tree a11 = new Tree("a11");
Tree a12 = new Tree("a12");
List l = new ArrayList();
l.add(a11);
l.add(a12);
a1.addChildren(l);
Tree t = new Tree("a1", new Tree[]{
new Tree("a11"),
new Tree("a12"),
});
assertEquals(t, a1);
}
public void testTreeSize(){
Tree t = new Tree("a1", new Tree[]{
new Tree("a11"),
new Tree("a12"),
});
assertEquals(2, t.size());
}
public void testTreeClearChildren(){
Tree t = new Tree("a1", new ITree[]{
new Tree("a11"),
new Tree("a12"),
});
t.clearChildren();
assertEquals(0, t.size());
}
public void testTreeVariableMatches(){
Tree t1 = new Tree("a1", new ITree[]{
new Tree("a11"),
new Tree("a12"),
});
Tree t2 = new Tree("a1", new ITree[]{
new Tree("a11"),
new TreeVariable("v"),
});
MatchContext ctx = new MatchContext();
boolean result = t2.matches(t1, ctx);
assertTrue(result);
ITree t3 = (ITree) ctx.get(new TreeVariable("v"));
assertEquals(new Tree("a12"), t3);
ITree t4 = (ITree) VariableReplacer.replace(t2, ctx);
assertEquals(t1, t4);
}
public void testTreeVariableMatches2(){
Tree t1 = new Tree("a1", new ITree[]{
new Tree("a11"),
new Tree("a12"),
});
Tree t2 = new Tree("a1", new ITree[]{
new Tree("a11"),
new TreeVariable("v"),
});
MatchContext ctx = new MatchContext();
boolean result = t1.matches(t2, ctx);
assertFalse(result);
}
}

View File

@ -0,0 +1,76 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.tree;
import java.util.*;
import com.ibm.wala.automaton.*;
import com.ibm.wala.automaton.tree.*;
public class TestTreeAutomatons extends TreeJunitBase {
/*
* t1[t11, t12[121,122], t13]
* <=> t1[t11[null; t12[t121[null; t122[null;null]]; t13[null; null]]]; null]
*/
public void testBinarization1() {
ITree t121 = new Tree("t121");
ITree t122 = new Tree("t122");
ITree t11 = new Tree("t11");
ITree t12 = new Tree("t12", new ITree[]{t121, t122});
ITree t13 = new Tree("t13");
ITree t1 = new Tree("t1", new ITree[]{t11, t12, t13});
IBinaryTree bt122 = new BinaryTree("t122");
IBinaryTree bt121 = new BinaryTree("t121", null, bt122);
IBinaryTree bt13 = new BinaryTree("t13");
IBinaryTree bt12 = new BinaryTree("t12", bt121, bt13);
IBinaryTree bt11 = new BinaryTree("t11", null, bt12);
IBinaryTree bt1 = new BinaryTree("t1", bt11, null);
IBinaryTree result1 = TreeAutomatons.binarize(t1);
assertEquals(bt1, result1);
ITree result2 = (ITree) TreeAutomatons.unbinarize(bt1).get(0);
assertEquals(t1, result2);
}
/*
* [t11, t12[121,122], t13]
* <=> t11[null; t12[t121[null; t122[null;null]]; t13[null; null]]]
*/
public void testBinarization2() {
ITree t121 = new Tree("t121");
ITree t122 = new Tree("t122");
ITree t11 = new Tree("t11");
ITree t12 = new Tree("t12", new ITree[]{t121, t122});
ITree t13 = new Tree("t13");
List l = AUtil.list(new ITree[]{t11,t12,t13});
IBinaryTree bt122 = new BinaryTree("t122");
IBinaryTree bt121 = new BinaryTree("t121", null, bt122);
IBinaryTree bt13 = new BinaryTree("t13");
IBinaryTree bt12 = new BinaryTree("t12", bt121, bt13);
IBinaryTree bt11 = new BinaryTree("t11", null, bt12);
IBinaryTree result1 = TreeAutomatons.binarize(l);
assertEquals(bt11, result1);
Collection result2 = TreeAutomatons.unbinarize(bt11);
assertEquals(l, result2);
}
}

View File

@ -0,0 +1,291 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.tree;
import java.util.*;
import com.ibm.wala.automaton.string.*;
import com.ibm.wala.automaton.tree.*;
public class TestTreeTransition extends TreeJunitBase {
public void testTreeTransitionPrePostState() {
StateBinaryTree input =
new StateBinaryTree(
new State("s1"),
new BinaryTree("t",
new BinaryTreeVariable("x"),
new BinaryTreeVariable("y")));
BinaryTree output =
new BinaryTree("u",
new StateBinaryTree(new State("s2"), new BinaryTreeVariable("y")),
new StateBinaryTree(new State("s3"), new BinaryTreeVariable("x")));
TreeTransition t = new TreeTransition(input, output);
IState pre = t.getPreState();
IState post = t.getPostState();
assertTrue(pre instanceof CompositeState);
assertTrue(post instanceof CompositeState);
CompositeState csPre = (CompositeState) pre;
CompositeState csPost = (CompositeState) post;
State s2 = new State("s2");
State s3 = new State("s3");
assertEquals(input.getState(), csPre.getStates().next());
for (Iterator i = csPost.getStates(); i.hasNext(); ) {
Object o = i.next();
assertTrue(o instanceof IState);
assertTrue(s2.equals(o) || s3.equals(o));
}
}
/*
* <s1>(null) -> (u[null;null])
*/
public void testTreeTransitionTransitLeaf() {
IBinaryTree input = new StateBinaryTree(new State("s1"), null);
IBinaryTree output = new BinaryTree("u");
TreeTransition t = new TreeTransition(input, output);
IBinaryTree result = t.transit(input);
assertEquals(output, result);
}
/*
* <s1>($x) -> (u[$x;$x])
*/
public void testTreeTransitionTransitWithVariableLeaf() {
BinaryTreeVariable x = new BinaryTreeVariable("x");
IBinaryTree input = new StateBinaryTree(new State("s1"), x);
IBinaryTree output = new BinaryTree("u", x, x);
TreeTransition t = new TreeTransition(input, output);
IBinaryTree result = t.transit(new StateBinaryTree(new State("s1"), BinaryTree.LEAF));
assertEquals(new BinaryTree("u"), result);
}
/*
* t[null;null] -> u[null;null]
*/
public void testTreeTransitionTransitSimple() {
IBinaryTree input = new BinaryTree("t");
IBinaryTree output = new BinaryTree("u");
TreeTransition t = new TreeTransition(input, output);
IBinaryTree result = t.transit(input);
assertEquals(output, result);
}
/*
* t[$x;$y] -> u[$y;$x]
*/
public void testTreeTransitionTransitWithVariable1() {
IBinaryTree input =
new BinaryTree("t",
new BinaryTreeVariable("x"),
new BinaryTreeVariable("y"));
IBinaryTree output =
new BinaryTree("u",
new BinaryTreeVariable("y"),
new BinaryTreeVariable("x"));
TreeTransition t = new TreeTransition(input, output);
IBinaryTree tree =
new BinaryTree("t",
new BinaryTree("t1"),
new BinaryTree("t2"));
IBinaryTree expected =
new BinaryTree("u",
new BinaryTree("t2"),
new BinaryTree("t1"));
IBinaryTree result = t.transit(tree);
assertEquals(expected, result);
}
/*
* <s1>(t[$x;$y]) -> u[<s2>($y); <s3>($x)]
*/
public void testTreeTransitionTransitWithStateVariable1() {
IBinaryTree input =
new StateBinaryTree(
new State("s1"),
new BinaryTree("t",
new BinaryTreeVariable("x"),
new BinaryTreeVariable("y")));
IBinaryTree output =
new BinaryTree("u",
new StateBinaryTree(new State("s2"), new BinaryTreeVariable("y")),
new StateBinaryTree(new State("s3"), new BinaryTreeVariable("x")));
TreeTransition t = new TreeTransition(input, output);
IBinaryTree tree =
new StateBinaryTree(
new State("s1"),
new BinaryTree("t",
new BinaryTree("t1"),
new BinaryTree("t2")));
IBinaryTree expected =
new BinaryTree("u",
new StateBinaryTree(new State("s2"), new BinaryTree("t2")),
new StateBinaryTree(new State("s3"), new BinaryTree("t1")));
IBinaryTree result = t.transit(tree);
assertEquals(expected, result);
}
/*
* u[<s2>($y); <s3>($x)] -> <s1>(t[$x;$y])
*/
public void testTreeTransitionTransitWithStateVariable2() {
IBinaryTree output =
new StateBinaryTree(
new State("s1"),
new BinaryTree("t",
new BinaryTreeVariable("x"),
new BinaryTreeVariable("y")));
IBinaryTree input =
new BinaryTree("u",
new StateBinaryTree(new State("s2"), new BinaryTreeVariable("y")),
new StateBinaryTree(new State("s3"), new BinaryTreeVariable("x")));
TreeTransition t = new TreeTransition(input, output);
IBinaryTree expected =
new StateBinaryTree(
new State("s1"),
new BinaryTree("t",
new BinaryTree("t1"),
new BinaryTree("t2")));
IBinaryTree tree =
new BinaryTree("u",
new StateBinaryTree(new State("s2"), new BinaryTree("t2")),
new StateBinaryTree(new State("s3"), new BinaryTree("t1")));
IBinaryTree result = t.transit(tree);
assertEquals(expected, result);
}
/*
* <s1>($t[$x;$y]) -> $t[<s2>($y); <s3>($x)]
*/
public void testFilteredTreeTransitionTransitWithStateVariable1() {
IBinaryTree input =
new StateBinaryTree(
new State("s1"),
new BinaryTree(new Variable("t"),
new BinaryTreeVariable("x"),
new BinaryTreeVariable("y")));
IBinaryTree output =
new BinaryTree(new Variable("t"),
new StateBinaryTree(new State("s2"), new BinaryTreeVariable("y")),
new StateBinaryTree(new State("s3"), new BinaryTreeVariable("x")));
FilteredTreeTransition t = new FilteredTreeTransition(input, output,
new FilteredTreeTransition.IFilter(){
public List invoke(ISymbol symbol, List outputs) {
IParentBinaryTree pbt = (IParentBinaryTree) outputs.get(0);
BinaryTree bt2 = new BinaryTree(pbt.getName().toUpperCase(), pbt.getLeft(), pbt.getRight());
List result = new ArrayList();
result.add(bt2);
return result;
}
},
new FilteredTreeTransition.ICondition(){
public boolean accept(ISymbol symbol, IMatchContext ctx) {
ISymbol label = (ISymbol) ctx.get(new Variable("t"));
if (label.getName().charAt(0) == 'a') {
return true;
}
else {
return false;
}
}
}
);
IBinaryTree tree1 =
new StateBinaryTree(
new State("s1"),
new BinaryTree("t",
new BinaryTree("t1"),
new BinaryTree("t2")));
IBinaryTree tree2 =
new StateBinaryTree(
new State("s1"),
new BinaryTree("abc",
new BinaryTree("t1"),
new BinaryTree("t2")));
IBinaryTree expected =
new BinaryTree("ABC",
new StateBinaryTree(new State("s2"), new BinaryTree("t2")),
new StateBinaryTree(new State("s3"), new BinaryTree("t1")));
assertFalse(t.accept(tree1, new MatchContext()));
assertTrue(t.accept(tree2, new MatchContext()));
IBinaryTree result = t.transit(tree2);
assertEquals(expected, result);
}
/*
* $t[<s2>($y); <s3>($x)] -> <s1>($t[$x;$y])
*/
public void testFilteredTreeTransitionTransitWithStateVariable2() {
IBinaryTree output =
new StateBinaryTree(
new State("s1"),
new BinaryTree(new Variable("t"),
new BinaryTreeVariable("x"),
new BinaryTreeVariable("y")));
IBinaryTree input =
new BinaryTree(new Variable("t"),
new StateBinaryTree(new State("s2"), new BinaryTreeVariable("y")),
new StateBinaryTree(new State("s3"), new BinaryTreeVariable("x")));
FilteredTreeTransition t = new FilteredTreeTransition(input, output,
new FilteredTreeTransition.IFilter(){
public List invoke(ISymbol symbol, List outputs) {
StateBinaryTree sbt = (StateBinaryTree) outputs.get(0);
IParentBinaryTree pbt = (IParentBinaryTree) sbt.getTree();
BinaryTree bt2 = new BinaryTree(pbt.getName().toUpperCase(), pbt.getLeft(), pbt.getRight());
StateBinaryTree sbt2 = new StateBinaryTree(sbt.getState(), bt2);
List result = new ArrayList();
result.add(sbt2);
return result;
}
},
new FilteredTreeTransition.ICondition(){
public boolean accept(ISymbol symbol, IMatchContext ctx) {
ISymbol label = (ISymbol) ctx.get(new Variable("t"));
if (label.getName().charAt(0) == 'a') {
return true;
}
else {
return false;
}
}
}
);
IBinaryTree tree1 =
new BinaryTree("u",
new StateBinaryTree(new State("s2"), new BinaryTree("t2")),
new StateBinaryTree(new State("s3"), new BinaryTree("t1")));
IBinaryTree tree2 =
new BinaryTree("abc",
new StateBinaryTree(new State("s2"), new BinaryTree("t2")),
new StateBinaryTree(new State("s3"), new BinaryTree("t1")));
IBinaryTree expected =
new StateBinaryTree(
new State("s1"),
new BinaryTree("ABC",
new BinaryTree("t1"),
new BinaryTree("t2")));
assertFalse(t.accept(tree1, new MatchContext()));
assertTrue(t.accept(tree2, new MatchContext()));
IBinaryTree result = t.transit(tree2);
assertEquals(expected, result);
}
}

View File

@ -0,0 +1,100 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.automaton.test.tree;
import java.util.*;
import com.ibm.wala.automaton.grammar.string.*;
import com.ibm.wala.automaton.grammar.tree.*;
import com.ibm.wala.automaton.string.ISymbol;
import com.ibm.wala.automaton.tree.*;
import junit.framework.TestCase;
abstract public class TreeJunitBase extends TestCase {
public ITreeVariable V(int n) {
return new TreeVariable("v" + n);
}
public ITreeVariable V(String name) {
return new TreeVariable(name);
}
public ITree T(String name, List children) {
return new Tree(name, children);
}
public ITree T(String name, ITree children[]) {
return new Tree(name, children);
}
public ITree T(String name, ITree t1) {
return new Tree(name, new ITree[]{t1});
}
public ITree T(String name, ITree t1, ITree t2) {
return new Tree(name, new ITree[]{t1, t2});
}
public ITree T(String name, ITree t1, ITree t2, ITree t3) {
return new Tree(name, new ITree[]{t1, t2, t3});
}
public ITree T(String name, ITree t1, ITree t2, ITree t3, ITree t4) {
return new Tree(name, new ITree[]{t1, t2, t3, t4});
}
public IBinaryTreeVariable BV(int n) {
return new BinaryTreeVariable("bv" + n);
}
public IBinaryTreeVariable BV(String name) {
return new BinaryTreeVariable(name);
}
public IBinaryTree BT(String name, IBinaryTree l, IBinaryTree r) {
return new BinaryTree(name, l, r);
}
public IBinaryTree BT(ISymbol label, IBinaryTree l, IBinaryTree r) {
return new BinaryTree(label, l, r);
}
public IBinaryTree BT(String name) {
return new BinaryTree(name);
}
public IBinaryTree BT(ISymbol label) {
return new BinaryTree(label);
}
public ITreeGrammar TG(IBinaryTreeVariable start, Object rules[]) {
Set l = new HashSet();
for (int i = 0; i < rules.length; i += 2) {
IBinaryTreeVariable left = (IBinaryTreeVariable) rules[i];
IBinaryTree right = (IBinaryTree) rules[i+1];
if (right == null) {
right = BinaryTree.LEAF;
}
IProductionRule rule = new ProductionRule(left, right);
l.add(rule);
}
return new TreeGrammar(start, l);
}
public void assertContains(ITreeGrammar g1, ITreeGrammar g2) {
assertTrue(RTLComparator.defaultComparator.contains(g1, g2));
}
public void assertNotContains(ITreeGrammar g1, ITreeGrammar g2) {
assertFalse(RTLComparator.defaultComparator.contains(g1, g2));
}
}