📄 logictransformer.java
字号:
/**
* Takes any And that has an Or as a child and rewrites it to move the Or
* upwards
*
* (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>
*/
class AndOrTransformation
implements
Transformation {
public GroupElement transform(final GroupElement and) throws InvalidPatternException {
final Or or = new Or();
determinePermutations( 0,
(And) and,
null,
or );
return or;
}
/**
* Recursive method that determins all unique combinations of children
* for the given parent and.
*
* @param currentLevel
* @param and
* @param combination
* @param or
*/
private void determinePermutations(final int currentLevel,
final And and,
And combination,
final Or or) {
final Object entry = and.getChildren().get( currentLevel );
if ( entry instanceof Or ) {
// Only OR nodes need to be iterated over
final Or childOr = (Or) entry;
for ( final Iterator it = childOr.getChildren().iterator(); it.hasNext(); ) {
// Make a temp copy of combinations+new entry which will be
// sent forward
final And temp = new And();
if ( currentLevel == 0 ) {
// Always start with a clean combination
combination = new And();
} else {
temp.getChildren().addAll( combination.getChildren() );
}
// now check for and remove duplicates
final Object object = it.next();
if ( object instanceof And ) {
// Can't have duplicate Ands so move up the children
final And childAnd = (And) object;
for ( final Iterator childIter = childAnd.getChildren().iterator(); childIter.hasNext(); ) {
temp.addChild( childIter.next() );
}
} else {
// no duplicates so just add
temp.addChild( object );
}
if ( currentLevel < and.getChildren().size() - 1 ) {
// keep recursing to build up the combination until we
// are at the end where it will be added to or
determinePermutations( currentLevel + 1,
and,
temp,
or );
} else {
// we are at the end so just attach the combination to
// the or node
or.addChild( temp );
}
}
} else {
// Make a temp copy of combinations+new entry which will be sent
// forward
final And temp = new And();
if ( currentLevel == 0 ) {
// Always start with a clean combination
combination = new And();
} else {
temp.getChildren().addAll( combination.getChildren() );
}
temp.addChild( entry );
if ( currentLevel < and.getChildren().size() - 1 ) {
// keep recursing to build up the combination until we are
// at the end where it will be added to or
determinePermutations( currentLevel + 1,
and,
temp,
or );
} else {
// we are at the end so just attach the combination to the
// or node
or.addChild( temp );
}
}
}
}
/**
* (Exist (OR (A B)
*
* <pre>
* Exist
* |
* or
* / \
* a b
* </pre>
*
* (Exist ( Not (a) Not (b)) )
*
* <pre>
* Exist
* / \
* Not Not
* | |
* a b
* </pre>
*/
class ExistOrTransformation
implements
Transformation {
public GroupElement transform(final GroupElement exist) throws InvalidPatternException {
throw new InvalidPatternException( "You cannot nest an OR within an Exists" );
// if ( !(exist.getChildren().get( 0 ) instanceof Or) ) {
// throw new RuntimeException( "ExistOrTransformation expected '" + Or.class.getName() + "' but instead found '" + exist.getChildren().get( 0 ).getClass().getName() + "'" );
// }
//
// /*
// * we know a Not only ever has one child, and the previous algorithm
// * has confirmed the child is an OR
// */
// Or or = (Or) exist.getChildren().get( 0 );
// And and = new And();
// for ( Iterator it = or.getChildren().iterator(); it.hasNext(); ) {
// Exists newExist = new Exists();
// newExist.addChild( it.next() );
// and.addChild( newExist );
// }
// return and;
}
}
/**
* (Not (OR (A B)
*
* <pre>
* Not
* |
* or
* / \
* a b
* </pre>
*
* (And ( Not (a) Exist (b)) )
*
* <pre>
* And
* / \
* Not Not
* | |
* a b
* </pre>
*/
public class NotOrTransformation
implements
Transformation {
public GroupElement transform(final GroupElement not) throws InvalidPatternException {
throw new InvalidPatternException( "You cannot nest an OR within an Not" );
// @todo for 3.1
// if ( !(not.getChildren().get( 0 ) instanceof Or) ) {
// throw new RuntimeException( "NotOrTransformation expected '" + Or.class.getName() + "' but instead found '" + not.getChildren().get( 0 ).getClass().getName() + "'" );
// }
//
// /*
// * we know a Not only ever has one child, and the previous algorithm
// * has confirmed the child is an OR
// */
// Or or = (Or) not.getChildren().get( 0 );
// And and = new And();
// for ( Iterator it = or.getChildren().iterator(); it.hasNext(); ) {
// Not newNot = new Not();
// newNot.addChild( it.next() );
// and.addChild( newNot );
// }
// return and;
}
}
//@todo for 3.1
// public class NotAndTransformation
// implements
// Transformation {
//
// public GroupElement transform(GroupElement not) throws InvalidPatternException {
// if ( !(not.getChildren().get( 0 ) instanceof And) ) {
// throw new RuntimeException( "NotAndTransformation expected '" + And.class.getName() + "' but instead found '" + not.getChildren().get( 0 ).getClass().getName() + "'" );
// }
//
// /*
// * we know a Not only ever has one child, and the previous algorithm
// * has confirmed the child is an And
// */
// And and = (And) not.getChildren().get( 0 );
// for ( Iterator it = and.getChildren().iterator(); it.hasNext(); ) {
// Object object1 = it.next();
//
// for ( Iterator it2 = and.getChildren().iterator(); it.hasNext(); ) {
// Object object2 = it.next();
// if ( object2 != object1 ) {
//
// }
// }
//
// Not newNot = new Not();
// newNot.addChild( it.next() );
// and.addChild( newNot );
// }
//
// return and;
// }
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -