Search in sources :

Example 21 with ObjectType

use of org.drools.core.spi.ObjectType in project drools by kiegroup.

the class PatternBuilder method getSettableProperties.

protected List<String> getSettableProperties(RuleBuildContext context, PatternDescr patternDescr, Pattern pattern) {
    ObjectType patternType = pattern.getObjectType();
    if (!(patternType instanceof ClassObjectType)) {
        return null;
    }
    Class<?> patternClass = patternType.getClassType();
    TypeDeclaration typeDeclaration = getTypeDeclaration(pattern, context);
    if (!typeDeclaration.isPropertyReactive()) {
        registerDescrBuildError(context, patternDescr, "Wrong usage of @" + Watch.class.getSimpleName() + " annotation on class " + patternClass.getName() + " that is not annotated as @PropertyReactive");
    }
    typeDeclaration.setTypeClass(patternClass);
    return typeDeclaration.getAccessibleProperties();
}
Also used : AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) Watch(org.kie.api.definition.rule.Watch) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 22 with ObjectType

use of org.drools.core.spi.ObjectType in project drools by kiegroup.

the class QueryTest method testDroolsQueryCleanup.

@Test
public void testDroolsQueryCleanup() throws Exception {
    KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_QueryMemoryLeak.drl"));
    KieSession session = createKieSession(kbase);
    KieSession ksession = kbase.newKieSession();
    String workerId = "B1234";
    Worker worker = new Worker();
    worker.setId(workerId);
    FactHandle handle = ksession.insert(worker);
    ksession.fireAllRules();
    assertNotNull(handle);
    Object retractedWorker = null;
    for (int i = 0; i < 100; i++) {
        retractedWorker = (Object) ksession.getQueryResults("getWorker", new Object[] { workerId });
    }
    assertNotNull(retractedWorker);
    StatefulKnowledgeSessionImpl sessionImpl = (StatefulKnowledgeSessionImpl) ksession;
    Collection<EntryPointNode> entryPointNodes = sessionImpl.getKnowledgeBase().getRete().getEntryPointNodes().values();
    EntryPointNode defaultEntryPointNode = null;
    for (EntryPointNode epNode : entryPointNodes) {
        if (epNode.getEntryPoint().getEntryPointId().equals("DEFAULT")) {
            defaultEntryPointNode = epNode;
            break;
        }
    }
    assertNotNull(defaultEntryPointNode);
    Map<ObjectType, ObjectTypeNode> obnodes = defaultEntryPointNode.getObjectTypeNodes();
    ObjectType key = new ClassObjectType(DroolsQuery.class);
    ObjectTypeNode droolsQueryNode = obnodes.get(key);
    Iterator<InternalFactHandle> it = ((ObjectTypeNodeMemory) sessionImpl.getNodeMemory(droolsQueryNode)).iterator();
    assertFalse(it.hasNext());
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) EntryPointNode(org.drools.core.reteoo.EntryPointNode) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) KieBase(org.kie.api.KieBase) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) Worker(org.drools.compiler.Worker) KieSession(org.kie.api.runtime.KieSession) DomainObject(org.drools.compiler.DomainObject) InsertedObject(org.drools.compiler.InsertedObject) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 23 with ObjectType

use of org.drools.core.spi.ObjectType in project drools by kiegroup.

the class LogicTransformerTest method testNotExistsTransformation.

/**
 * Exists inside a not is redundant and should be eliminated
 *
 * (Not (exists (A) ) )
 *
 * <pre>
 *             Not
 *              |
 *            Exists
 *              |
 *             And
 *             / \
 *            a   b
 * </pre>
 *
 * Should become:
 *
 * <pre>
 *             Not
 *              |
 *             And
 *             / \
 *            a   b
 * </pre>
 */
@Test
public void testNotExistsTransformation() 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 GroupElement not = GroupElementFactory.newNotInstance();
    final GroupElement exists = GroupElementFactory.newExistsInstance();
    final GroupElement and = GroupElementFactory.newAndInstance();
    not.addChild(exists);
    exists.addChild(and);
    and.addChild(a);
    and.addChild(b);
    GroupElement[] transformed = LogicTransformer.getInstance().transform(not, Collections.EMPTY_MAP);
    GroupElement wrapper = transformed[0];
    GroupElement notR = (GroupElement) wrapper.getChildren().get(0);
    assertTrue(notR.isNot());
    assertEquals(1, notR.getChildren().size());
    assertTrue(notR.getChildren().get(0) instanceof GroupElement);
    GroupElement andR = (GroupElement) notR.getChildren().get(0);
    assertTrue(andR.isAnd());
    assertEquals(2, andR.getChildren().size());
    assertTrue(andR.getChildren().get(0) instanceof Pattern);
    assertTrue(andR.getChildren().get(1) instanceof Pattern);
    final Pattern a1 = (Pattern) andR.getChildren().get(0);
    final Pattern b1 = (Pattern) andR.getChildren().get(1);
    assertEquals(a, a1);
    assertEquals(b, b1);
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) Test(org.junit.Test)

Example 24 with ObjectType

use of org.drools.core.spi.ObjectType in project drools by kiegroup.

the class LogicTransformerTest method testCloneable.

@Test
public void testCloneable() {
    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");
    // Test against a known false tree
    final GroupElement and = GroupElementFactory.newAndInstance();
    and.addChild(a);
    and.addChild(b);
    final GroupElement or = GroupElementFactory.newOrInstance();
    or.addChild(c);
    or.addChild(d);
    and.addChild(or);
    final GroupElement and2 = GroupElementFactory.newAndInstance();
    and2.addChild(e);
    and2.addChild(f);
    or.addChild(and2);
    final GroupElement not = GroupElementFactory.newNotInstance();
    and.addChild(not);
    final GroupElement or2 = GroupElementFactory.newOrInstance();
    not.addChild(or2);
    or2.addChild(g);
    or2.addChild(h);
    final GroupElement cloned = (GroupElement) and.clone();
    assertEquals(and, cloned);
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) Test(org.junit.Test)

Example 25 with ObjectType

use of org.drools.core.spi.ObjectType in project drools by kiegroup.

the class LogicTransformerTest method testMultipleOrAndOrTransformation.

/**
 * (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>
 */
@Test
public void testMultipleOrAndOrTransformation() 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 Pattern d = new Pattern(3, type, "d");
    final Pattern e = new Pattern(4, type, "e");
    final Pattern f = new Pattern(5, type, "f");
    final GroupElement parent = GroupElementFactory.newAndInstance();
    final GroupElement or = GroupElementFactory.newOrInstance();
    or.addChild(a);
    or.addChild(b);
    parent.addChild(or);
    parent.addChild(c);
    final GroupElement or2 = GroupElementFactory.newOrInstance();
    or2.addChild(d);
    or2.addChild(e);
    parent.addChild(or2);
    final GroupElement not = GroupElementFactory.newNotInstance();
    not.addChild(f);
    parent.addChild(not);
    LogicTransformer.getInstance().applyOrTransformation(parent);
    assertEquals(GroupElement.Type.OR, parent.getType());
    assertLength(4, parent.getChildren());
    assertEquals(GroupElement.class, parent.getChildren().get(0).getClass());
    assertEquals(GroupElement.class, parent.getChildren().get(1).getClass());
    assertEquals(GroupElement.class, parent.getChildren().get(2).getClass());
    assertEquals(GroupElement.class, parent.getChildren().get(3).getClass());
    GroupElement and1 = (GroupElement) parent.getChildren().get(0);
    assertTrue(and1.isAnd());
    assertLength(4, and1.getChildren());
    assertEquals(a, and1.getChildren().get(0));
    assertEquals(c, and1.getChildren().get(1));
    assertEquals(d, and1.getChildren().get(2));
    assertEquals(not, and1.getChildren().get(3));
    and1 = (GroupElement) parent.getChildren().get(1);
    assertTrue(and1.isAnd());
    assertLength(4, and1.getChildren());
    assertEquals(a, and1.getChildren().get(0));
    assertEquals(c, and1.getChildren().get(1));
    assertEquals(e, and1.getChildren().get(2));
    assertEquals(not, and1.getChildren().get(3));
    and1 = (GroupElement) parent.getChildren().get(2);
    assertTrue(and1.isAnd());
    assertLength(4, and1.getChildren());
    assertEquals(b, and1.getChildren().get(0));
    assertEquals(c, and1.getChildren().get(1));
    assertEquals(d, and1.getChildren().get(2));
    assertEquals(not, and1.getChildren().get(3));
    and1 = (GroupElement) parent.getChildren().get(3);
    assertTrue(and1.isAnd());
    assertLength(4, and1.getChildren());
    assertEquals(b, and1.getChildren().get(0));
    assertEquals(c, and1.getChildren().get(1));
    assertEquals(e, and1.getChildren().get(2));
    assertEquals(not, and1.getChildren().get(3));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) Test(org.junit.Test)

Aggregations

ObjectType (org.drools.core.spi.ObjectType)33 ClassObjectType (org.drools.core.base.ClassObjectType)32 Test (org.junit.Test)17 Pattern (org.drools.core.rule.Pattern)13 Declaration (org.drools.core.rule.Declaration)7 FactTemplateObjectType (org.drools.core.facttemplates.FactTemplateObjectType)5 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)5 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)5 InternalFactHandle (org.drools.core.common.InternalFactHandle)4 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)4 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)4 TypeDeclaration (org.drools.core.rule.TypeDeclaration)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)3 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)3 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)3 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)3 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)3