Search in sources :

Example 1 with FieldTemplateImpl

use of org.drools.core.facttemplates.FieldTemplateImpl in project drools by kiegroup.

the class PatternTest method testDeclarationsFactTemplate.

@Test
public void testDeclarationsFactTemplate() throws Exception {
    InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("org.store");
    final FieldTemplate cheeseName = new FieldTemplateImpl("name", String.class);
    final FieldTemplate cheesePrice = new FieldTemplateImpl("price", 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();
    stilton.set("name", "stilton");
    stilton.set("price", new Integer(200));
    assertEquals(stilton, dec.getValue(null, stilton));
}
Also used : Pattern(org.drools.core.rule.Pattern) FactTemplateImpl(org.drools.core.facttemplates.FactTemplateImpl) FieldTemplate(org.drools.core.facttemplates.FieldTemplate) FieldTemplateImpl(org.drools.core.facttemplates.FieldTemplateImpl) Fact(org.drools.core.facttemplates.Fact) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) Declaration(org.drools.core.rule.Declaration) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) FactTemplate(org.drools.core.facttemplates.FactTemplate) Test(org.junit.Test)

Example 2 with FieldTemplateImpl

use of org.drools.core.facttemplates.FieldTemplateImpl in project drools by kiegroup.

the class Misc2Test method testFactTemplates.

@Test
public void testFactTemplates() {
    // DROOLS-600
    String drl = "package com.testfacttemplate;" + " rule \"test rule\" " + " dialect \"mvel\" " + " when " + " $test : TestFactTemplate( status == 1 ) " + " then " + " System.out.println( \"Hello World\" ); " + " end ";
    KnowledgePackageImpl kPackage = new KnowledgePackageImpl("com.testfacttemplate");
    FieldTemplate fieldTemplate = new FieldTemplateImpl("status", 0, Integer.class);
    FactTemplate factTemplate = new FactTemplateImpl(kPackage, "TestFactTemplate", new FieldTemplate[] { fieldTemplate });
    KnowledgeBuilder kBuilder = new KnowledgeBuilderImpl(kPackage);
    StringReader rule = new StringReader(drl);
    try {
        ((KnowledgeBuilderImpl) kBuilder).addPackageFromDrl(rule);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : FactTemplateImpl(org.drools.core.facttemplates.FactTemplateImpl) FieldTemplate(org.drools.core.facttemplates.FieldTemplate) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) FieldTemplateImpl(org.drools.core.facttemplates.FieldTemplateImpl) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) FactTemplate(org.drools.core.facttemplates.FactTemplate) Test(org.junit.Test)

Example 3 with FieldTemplateImpl

use of org.drools.core.facttemplates.FieldTemplateImpl 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));
}
Also used : FactTemplateImpl(org.drools.core.facttemplates.FactTemplateImpl) FieldTemplate(org.drools.core.facttemplates.FieldTemplate) FieldTemplateImpl(org.drools.core.facttemplates.FieldTemplateImpl) Fact(org.drools.core.facttemplates.Fact) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) FactTemplate(org.drools.core.facttemplates.FactTemplate) Test(org.junit.Test)

Example 4 with FieldTemplateImpl

use of org.drools.core.facttemplates.FieldTemplateImpl in project drools by kiegroup.

the class FactFactory method prototypeToFactTemplate.

public static FactTemplate prototypeToFactTemplate(Prototype prototype, InternalKnowledgePackage pkg) {
    FieldTemplate[] fieldTemplates = new FieldTemplate[prototype.getFieldNames().size()];
    int i = 0;
    for (String fieldName : prototype.getFieldNames()) {
        fieldTemplates[i++] = new FieldTemplateImpl(fieldName, prototype.getField(fieldName).getType());
    }
    return new FactTemplateImpl(pkg, prototype.getName(), fieldTemplates);
}
Also used : FactTemplateImpl(org.drools.core.facttemplates.FactTemplateImpl) FieldTemplate(org.drools.core.facttemplates.FieldTemplate) FieldTemplateImpl(org.drools.core.facttemplates.FieldTemplateImpl)

Example 5 with FieldTemplateImpl

use of org.drools.core.facttemplates.FieldTemplateImpl in project drools by kiegroup.

the class Misc2Test method testFactTemplates.

@Test
public void testFactTemplates() {
    // DROOLS-600
    String drl = "package com.testfacttemplate;" + " rule \"test rule\" " + " dialect \"mvel\" " + " when " + " $test : TestFactTemplate( status == 1 ) " + " then " + " System.out.println( \"Hello World\" ); " + " end ";
    InternalKnowledgePackage kPackage = CoreComponentFactory.get().createKnowledgePackage("com.testfacttemplate");
    FieldTemplate fieldTemplate = new FieldTemplateImpl("status", Integer.class);
    FactTemplate factTemplate = new FactTemplateImpl(kPackage, "TestFactTemplate", new FieldTemplate[] { fieldTemplate });
    KnowledgeBuilder kBuilder = new KnowledgeBuilderImpl(kPackage);
    StringReader rule = new StringReader(drl);
    try {
        ((KnowledgeBuilderImpl) kBuilder).addPackageFromDrl(rule);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : FactTemplateImpl(org.drools.core.facttemplates.FactTemplateImpl) FieldTemplate(org.drools.core.facttemplates.FieldTemplate) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) FieldTemplateImpl(org.drools.core.facttemplates.FieldTemplateImpl) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) FactWithString(org.drools.mvel.integrationtests.facts.FactWithString) DroolsParserException(org.drools.drl.parser.DroolsParserException) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) FactTemplate(org.drools.core.facttemplates.FactTemplate) Test(org.junit.Test)

Aggregations

FactTemplateImpl (org.drools.core.facttemplates.FactTemplateImpl)5 FieldTemplate (org.drools.core.facttemplates.FieldTemplate)5 FieldTemplateImpl (org.drools.core.facttemplates.FieldTemplateImpl)5 FactTemplate (org.drools.core.facttemplates.FactTemplate)4 Test (org.junit.Test)4 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)3 StringReader (java.io.StringReader)2 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)2 ClassObjectType (org.drools.core.base.ClassObjectType)2 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)2 Fact (org.drools.core.facttemplates.Fact)2 FactTemplateObjectType (org.drools.core.facttemplates.FactTemplateObjectType)2 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)2 ObjectType (org.drools.core.spi.ObjectType)2 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)2 DroolsParserException (org.drools.compiler.compiler.DroolsParserException)1 Declaration (org.drools.core.rule.Declaration)1 Pattern (org.drools.core.rule.Pattern)1 DroolsParserException (org.drools.drl.parser.DroolsParserException)1 FactWithString (org.drools.mvel.integrationtests.facts.FactWithString)1