📄 logictransformertest.java
字号:
package org.drools.rule;
/*
* Copyright 2005 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.drools.DroolsTestCase;
public class LogicTransformerTest extends DroolsTestCase {
/**
* (a||b)&&c
*
* <pre>
* and
* / \
* or c
* / \
* a b
* </pre>
*
* Should become (a&&c)||(b&&c)
*
* <pre>
*
* or
* / \
* / \
* / \
* and and
* / \ / \
* a c b c
* </pre>
*/
public void testSingleOrAndOrTransformation() throws InvalidPatternException {
final String a = "a";
final String b = "b";
final String c = "c";
final And and = new And();
and.addChild( c );
final Or or = new Or();
or.addChild( a );
or.addChild( b );
and.addChild( or );
final Or newOr = (Or) LogicTransformer.getInstance().applyOrTransformation( and,
or );
assertLength( 2,
newOr.getChildren() );
assertEquals( And.class,
newOr.getChildren().get( 0 ).getClass() );
assertEquals( And.class,
newOr.getChildren().get( 1 ).getClass() );
final And and1 = (And) newOr.getChildren().get( 0 );
assertContains( c,
and1.getChildren() );
assertContains( a,
and1.getChildren() );
final And and2 = (And) newOr.getChildren().get( 1 );
assertContains( c,
and2.getChildren() );
assertContains( b,
and2.getChildren() );
}
/**
* (a||b)&&c
*
* <pre>
* And
* /|\ \__
* _/ | \_ \_
* / | \ \
* or | or not
* / \ | / \ |
* a b c d e f
* </pre>
*
* Should become (a&&c)||(b&&c)
*
* <pre>
* /\
* _/ \_
* / \
* _/| |\_
* __/ | | \__
* __/ | | \__
* / | | \
* and and and and
* /||\ /||\ /||\ /||\
* a cd Not a ce Not b cd Not b ce Not
* | | | |
* f f f f
* </pre>
*/
public void testMultipleOrAndOrTransformation() throws InvalidPatternException {
final String a = "a";
final String b = "b";
final String c = "c";
final String d = "d";
final String e = "e";
final String f = "f";
final And and = new And();
final Or or = new Or();
or.addChild( a );
or.addChild( b );
and.addChild( or );
and.addChild( c );
final Or or2 = new Or();
or2.addChild( d );
or2.addChild( e );
and.addChild( or2 );
final Not not = new Not();
not.addChild( f );
and.addChild( not );
final Or newOr = (Or) LogicTransformer.getInstance().applyOrTransformation( and,
or );
assertLength( 4,
newOr.getChildren() );
assertEquals( And.class,
newOr.getChildren().get( 0 ).getClass() );
assertEquals( And.class,
newOr.getChildren().get( 1 ).getClass() );
assertEquals( And.class,
newOr.getChildren().get( 2 ).getClass() );
assertEquals( And.class,
newOr.getChildren().get( 3 ).getClass() );
And and1 = (And) newOr.getChildren().get( 0 );
assertLength( 4,
and1.getChildren() );
assertContains( a,
and1.getChildren() );
assertContains( c,
and1.getChildren() );
assertContains( d,
and1.getChildren() );
assertContains( not,
and1.getChildren() );
and1 = (And) newOr.getChildren().get( 1 );
assertLength( 4,
and1.getChildren() );
assertContains( a,
and1.getChildren() );
assertContains( c,
and1.getChildren() );
assertContains( e,
and1.getChildren() );
assertContains( not,
and1.getChildren() );
and1 = (And) newOr.getChildren().get( 2 );
assertLength( 4,
and1.getChildren() );
assertContains( b,
and1.getChildren() );
assertContains( c,
and1.getChildren() );
assertContains( d,
and1.getChildren() );
assertContains( not,
and1.getChildren() );
and1 = (And) newOr.getChildren().get( 3 );
assertLength( 4,
and1.getChildren() );
assertContains( b,
and1.getChildren() );
assertContains( c,
and1.getChildren() );
assertContains( e,
and1.getChildren() );
assertContains( not,
and1.getChildren() );
}
/**
* This data structure is now valid
*
* (Not (OR (A B)
*
* <pre>
* Not
* |
* or
* / \
* a b
* </pre>
*
*/
public void xxxtestNotOrTransformation() throws InvalidPatternException {
final String a = "a";
final String b = "b";
final Not not = new Not();
final Or or = new Or();
not.addChild( or );
or.addChild( a );
or.addChild( b );
try {
final And newAnd = (And) LogicTransformer.getInstance().applyOrTransformation( not,
or );
fail( "This should fail as you cannot nest Ors under Nots" );
} catch ( final InvalidPatternException e ) {
//
}
}
/**
* This data structure is not valid (Exists (OR (A B)
*
* <pre>
* Exists
* |
* or
* / \
* a b
* </pre>
*
*/
public void xxxtestExistOrTransformation() throws InvalidPatternException {
final String a = "a";
final String b = "b";
final Exists exist = new Exists();
final Or or = new Or();
exist.addChild( or );
or.addChild( a );
or.addChild( b );
try {
final And newAnd = (And) LogicTransformer.getInstance().applyOrTransformation( exist,
or );
fail( "This should fail as you cannot nest Ors under Existss" );
} catch ( final InvalidPatternException e ) {
//
}
}
public void testDuplicatTransformation() throws InvalidRuleException {
final String a = "a";
final String b = "b";
final String c = "c";
final String d = "d";
final And and1 = new And();
and1.addChild( a );
and1.addChild( b );
final And and2 = new And();
and2.addChild( c );
and2.addChild( d );
and1.addChild( and2 );
final Or or = new Or();
and1.addChild( or );
LogicTransformer.getInstance().checkForAndRemoveDuplicates( and1 );
assertLength( 5,
and1.getChildren() );
assertContains( a,
and1.getChildren() );
assertContains( b,
and1.getChildren() );
assertContains( c,
and1.getChildren() );
assertContains( d,
and1.getChildren() );
assertContains( or,
and1.getChildren() );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -