use of org.drools.core.spi.ObjectType in project drools by kiegroup.
the class LogicTransformerTest method testSingleOrAndOrTransformation.
/**
* (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>
*/
@Test
public void testSingleOrAndOrTransformation() throws InvalidPatternException {
final ObjectType type = new ClassObjectType(String.class);
final Pattern a = new Pattern(0, type, "a");
final Pattern b = new Pattern(1, type, "b");
final Pattern c = new Pattern(2, type, "c");
final GroupElement or = GroupElementFactory.newOrInstance();
or.addChild(a);
or.addChild(b);
final GroupElement parent = GroupElementFactory.newAndInstance();
parent.addChild(or);
parent.addChild(c);
LogicTransformer.getInstance().applyOrTransformation(parent);
assertLength(2, parent.getChildren());
assertEquals(GroupElement.class, parent.getChildren().get(0).getClass());
assertEquals(GroupElement.class, parent.getChildren().get(1).getClass());
final GroupElement and1 = (GroupElement) parent.getChildren().get(0);
assertTrue(and1.isAnd());
// transformation MUST keep the order
assertEquals(a, and1.getChildren().get(0));
assertEquals(c, and1.getChildren().get(1));
final GroupElement and2 = (GroupElement) parent.getChildren().get(1);
assertEquals(b, and2.getChildren().get(0));
assertEquals(c, and2.getChildren().get(1));
}
use of org.drools.core.spi.ObjectType in project drools by kiegroup.
the class LogicTransformerTest method testTransform.
/**
* /**
*
* <pre>
* _/|\_
* __/ | \__
* / | \
* __/ | \__
* / | \
* And or And
* / \ / \ / \
* a Or d e Not OR
* / \ | / |
* b c f g Not
* |
* h
*
* </pre>
*
* Each And is a Rete sub rule
*
* <pre>
*
* And___ And___ And___ And___ And__ And___ And___ And___
* ||| | \ ||| | \ ||| | \ ||| | \ ||| | \ ||| | \ ||| | \ ||| | \
* abd Not g abd Not Not abe Not g abe Not Not acd Not g acd Not Not ace Not g ace Not Not
* | | | | | | | | | | | |
* f f h f f h f f h f f h
*
* </pre>
*
* @throws IOException
* @throws ClassNotFoundException
*
* @throws IOException
* @throws ClassNotFoundException
*/
@Test
public void testTransform() throws IOException, ClassNotFoundException, InvalidPatternException {
final ObjectType type = new ClassObjectType(String.class);
final Pattern a = new Pattern(0, type, "a");
final Pattern b = new Pattern(1, type, "b");
final Pattern c = new Pattern(2, type, "c");
final Pattern d = new Pattern(3, type, "d");
final Pattern e = new Pattern(4, type, "e");
final Pattern f = new Pattern(5, type, "f");
final Pattern g = new Pattern(6, type, "g");
final Pattern h = new Pattern(7, type, "h");
final GroupElement and = GroupElementFactory.newAndInstance();
final GroupElement and1 = GroupElementFactory.newAndInstance();
and1.addChild(a);
final GroupElement or1 = GroupElementFactory.newOrInstance();
or1.addChild(b);
or1.addChild(c);
and1.addChild(or1);
and.addChild(and1);
final GroupElement or2 = GroupElementFactory.newOrInstance();
or2.addChild(d);
or2.addChild(e);
and.addChild(or2);
final GroupElement and2 = GroupElementFactory.newAndInstance();
final GroupElement not1 = GroupElementFactory.newNotInstance();
not1.addChild(f);
final GroupElement or3 = GroupElementFactory.newOrInstance();
or3.addChild(g);
final GroupElement not2 = GroupElementFactory.newNotInstance();
not2.addChild(h);
or3.addChild(not2);
and2.addChild(not1);
and2.addChild(or3);
and.addChild(and2);
final GroupElement[] ands = LogicTransformer.getInstance().transform(and, Collections.EMPTY_MAP);
// Uncomment this when you need to output a new known correct tree
// result
final File testFile = new File("target/test/LogicTransformerTest_correct_transform1.dat");
testFile.getParentFile().mkdirs();
DroolsStreamUtils.streamOut(new FileOutputStream(testFile), ands);
// Now check the main tree
// Get known correct tree
// The binary stream was created from a handchecked correct output
final GroupElement[] correctResultAnds = (GroupElement[]) DroolsStreamUtils.streamIn(new FileInputStream(testFile));
for (int j = 0; j < ands.length; j++) {
assertEquals(correctResultAnds[j], ands[j]);
}
}
use of org.drools.core.spi.ObjectType in project drools by kiegroup.
the class PatternTest method testDeclarationsFactTemplate.
@Test
public void testDeclarationsFactTemplate() throws Exception {
InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.store");
final FieldTemplate cheeseName = new FieldTemplateImpl("name", 0, String.class);
final FieldTemplate cheesePrice = new FieldTemplateImpl("price", 1, Integer.class);
final FieldTemplate[] fields = new FieldTemplate[] { cheeseName, cheesePrice };
final FactTemplate cheese = new FactTemplateImpl(pkg, "Cheese", fields);
final ObjectType type = new FactTemplateObjectType(cheese);
final Pattern col = new Pattern(0, type, "foo");
final Declaration dec = col.getDeclaration();
final InternalReadAccessor ext = dec.getExtractor();
assertEquals(Fact.class, ext.getExtractToClass());
final Fact stilton = cheese.createFact(10);
stilton.setFieldValue("name", "stilton");
stilton.setFieldValue("price", new Integer(200));
assertEquals(stilton, dec.getValue(null, stilton));
}
use of org.drools.core.spi.ObjectType in project drools by kiegroup.
the class PatternTest method testDeclarationsObjectType.
@Test
public void testDeclarationsObjectType() throws Exception {
final ObjectType type = new ClassObjectType(Cheese.class);
final Pattern col = new Pattern(0, type, "foo");
final Declaration dec = col.getDeclaration();
final InternalReadAccessor ext = dec.getExtractor();
assertEquals(Cheese.class, ext.getExtractToClass());
final Cheese stilton = new Cheese("stilton", 42);
assertEquals(stilton, dec.getValue(null, stilton));
}
use of org.drools.core.spi.ObjectType in project drools by kiegroup.
the class AbstractTerminalNode method initDeclaredMask.
public void initDeclaredMask(BuildContext context) {
if (!(unwrapTupleSource() instanceof LeftInputAdapterNode)) {
// RTN's not after LIANode are not relevant for property specific, so don't block anything.
setDeclaredMask(AllSetBitMask.get());
return;
}
Pattern pattern = context.getLastBuiltPatterns()[0];
ObjectType objectType = pattern.getObjectType();
if (!(objectType instanceof ClassObjectType)) {
// InitialFact has no type declaration and cannot be property specific
// Only ClassObjectType can use property specific
setDeclaredMask(AllSetBitMask.get());
return;
}
Class objectClass = ((ClassObjectType) objectType).getClassType();
TypeDeclaration typeDeclaration = context.getKnowledgeBase().getTypeDeclaration(objectClass);
if (typeDeclaration == null || !typeDeclaration.isPropertyReactive()) {
// if property specific is not on, then accept all modification propagations
setDeclaredMask(AllSetBitMask.get());
} else {
List<String> settableProperties = getAccessibleProperties(context.getKnowledgeBase(), objectClass);
Class modifiedClass = ((ClassObjectType) pattern.getObjectType()).getClassType();
setDeclaredMask(calculatePositiveMask(modifiedClass, pattern.getListenedProperties(), settableProperties));
setNegativeMask(calculateNegativeMask(modifiedClass, pattern.getListenedProperties(), settableProperties));
}
}
Aggregations