Search in sources :

Example 1 with ImportDescr

use of org.drools.drl.ast.descr.ImportDescr in project drools by kiegroup.

the class PackageDescrTest method testSerializationImportDescr.

@Test
public void testSerializationImportDescr() {
    PackageDescrBuilder builder = DescrFactory.newPackage().name("foo");
    String className = Person.class.getName();
    builder.newImport().target(className).end();
    PackageDescr descr = builder.getDescr();
    ImportDescr importDescr = new ImportDescr(className);
    ImportDescr badImportDescr = new ImportDescr(null);
    assertTrue(descr.getImports().contains(importDescr));
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(baos);
        descr.writeExternal(out);
        ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
        PackageDescr newDescr = new PackageDescr();
        newDescr.readExternal(in);
        assertFalse(newDescr.getImports().contains(badImportDescr));
        assertTrue(newDescr.getImports().contains(importDescr));
        assertFalse(newDescr.getPreferredPkgUUID().isPresent());
    } catch (IOException | ClassNotFoundException e) {
        fail(e.getMessage());
    }
}
Also used : PackageDescrBuilder(org.drools.drl.ast.dsl.PackageDescrBuilder) ObjectOutput(java.io.ObjectOutput) ImportDescr(org.drools.drl.ast.descr.ImportDescr) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(java.io.ObjectInput) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 2 with ImportDescr

use of org.drools.drl.ast.descr.ImportDescr in project drools by kiegroup.

the class PackageBuilderConfigurationTest method testMockDialect.

@Test
public void testMockDialect() {
    InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("org.pkg1");
    KnowledgeBuilderConfigurationImpl cfg1 = new KnowledgeBuilderConfigurationImpl();
    MockDialectConfiguration mockConf = new MockDialectConfiguration();
    // cfg1.buildDialectRegistry().addDialect( "mock",
    // mockConf.getDialect() );
    cfg1.addDialect("mock", mockConf);
    cfg1.setDefaultDialect("mock");
    KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl(pkg, cfg1);
    PackageRegistry pkgRegistry = builder.getPackageRegistry(pkg.getName());
    DialectCompiletimeRegistry dialectRegistry = pkgRegistry.getDialectCompiletimeRegistry();
    MockDialect mockDialect2 = (MockDialect) dialectRegistry.getDialect(cfg1.getDefaultDialect());
    assertSame(pkg, mockDialect2.getPkg());
    assertNull(mockDialect2.getRuleDescr());
    RuleDescr ruleDescr = new RuleDescr("test rule");
    ruleDescr.addAttribute(new AttributeDescr("dialect", "mock"));
    ruleDescr.setLhs(new AndDescr());
    EvalDescr evalDescr = new EvalDescr();
    ruleDescr.getLhs().addDescr(evalDescr);
    PackageDescr pkgDescr = new PackageDescr("org.pkg1");
    pkgDescr.addImport(new ImportDescr("java.util.HashMap"));
    FunctionImportDescr functionImportDescr = new FunctionImportDescr();
    functionImportDescr.setTarget("java.lang.System.currentTimeMillis");
    pkgDescr.addFunctionImport(functionImportDescr);
    pkgDescr.addRule(ruleDescr);
    builder.addPackage(pkgDescr);
    assertSame(ruleDescr, mockDialect2.getRuleDescr());
    assertTrue(mockDialect2.getImport().contains("java.util.HashMap"));
    assertTrue(mockDialect2.getStaticImport().contains("java.lang.System.currentTimeMillis"));
    assertEquals("eval was built", evalDescr.getContent());
    assertEquals("consequence was built", ruleDescr.getConsequence());
    assertTrue(mockDialect2.isCompileAll());
    assertNotNull(pkg.getRule("test rule"));
    // make sure there were no other general errors.
    assertFalse(builder.hasErrors());
}
Also used : FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) AndDescr(org.drools.drl.ast.descr.AndDescr) ImportDescr(org.drools.drl.ast.descr.ImportDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) EvalDescr(org.drools.drl.ast.descr.EvalDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 3 with ImportDescr

use of org.drools.drl.ast.descr.ImportDescr in project drools by kiegroup.

the class RuleParserTest method testGlobal1.

@Test
public void testGlobal1() throws Exception {
    final String source = "package foo.bar.baz\n" + "import com.foo.Bar\n" + "global java.util.List<java.util.Map<String,Integer>> aList;\n" + "global Integer aNumber";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertEquals("foo.bar.baz", pkg.getName());
    assertEquals(1, pkg.getImports().size());
    ImportDescr impdescr = pkg.getImports().get(0);
    assertEquals("com.foo.Bar", impdescr.getTarget());
    assertEquals(source.indexOf("import " + impdescr.getTarget()), impdescr.getStartCharacter());
    assertEquals(source.indexOf("import " + impdescr.getTarget()) + ("import " + impdescr.getTarget()).length(), impdescr.getEndCharacter());
    assertEquals(2, pkg.getGlobals().size());
    GlobalDescr global = pkg.getGlobals().get(0);
    assertEquals("java.util.List<java.util.Map<String,Integer>>", global.getType());
    assertEquals("aList", global.getIdentifier());
    assertEquals(source.indexOf("global " + global.getType()), global.getStartCharacter());
    assertEquals(source.indexOf("global " + global.getType() + " " + global.getIdentifier()) + ("global " + global.getType() + " " + global.getIdentifier()).length(), global.getEndCharacter());
    global = pkg.getGlobals().get(1);
    assertEquals("Integer", global.getType());
    assertEquals("aNumber", global.getIdentifier());
    assertEquals(source.indexOf("global " + global.getType()), global.getStartCharacter());
    assertEquals(source.indexOf("global " + global.getType() + " " + global.getIdentifier()) + ("global " + global.getType() + " " + global.getIdentifier()).length(), global.getEndCharacter());
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) ImportDescr(org.drools.drl.ast.descr.ImportDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) AccumulateImportDescr(org.drools.drl.ast.descr.AccumulateImportDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 4 with ImportDescr

use of org.drools.drl.ast.descr.ImportDescr in project drools by kiegroup.

the class RuleParserTest method testFunctionImport.

@Test
public void testFunctionImport() throws Exception {
    final String source = "package foo\n" + "import function java.lang.Math.max\n" + "import function java.lang.Math.min;\n" + "import foo.bar.*\n" + "import baz.Baz";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertEquals("foo", pkg.getName());
    assertEquals(2, pkg.getImports().size());
    ImportDescr impdescr = pkg.getImports().get(0);
    assertEquals("foo.bar.*", impdescr.getTarget());
    assertEquals(source.indexOf("import " + impdescr.getTarget()), impdescr.getStartCharacter());
    assertEquals(source.indexOf("import " + impdescr.getTarget()) + ("import " + impdescr.getTarget()).length(), impdescr.getEndCharacter());
    impdescr = pkg.getImports().get(1);
    assertEquals("baz.Baz", impdescr.getTarget());
    assertEquals(source.indexOf("import " + impdescr.getTarget()), impdescr.getStartCharacter());
    assertEquals(source.indexOf("import " + impdescr.getTarget()) + ("import " + impdescr.getTarget()).length(), impdescr.getEndCharacter());
    assertEquals(2, pkg.getFunctionImports().size());
    impdescr = pkg.getFunctionImports().get(0);
    assertEquals("java.lang.Math.max", impdescr.getTarget());
    assertEquals(source.indexOf("import function " + impdescr.getTarget()), impdescr.getStartCharacter());
    assertEquals(source.indexOf("import function " + impdescr.getTarget()) + ("import function " + impdescr.getTarget()).length(), impdescr.getEndCharacter());
    impdescr = pkg.getFunctionImports().get(1);
    assertEquals("java.lang.Math.min", impdescr.getTarget());
    assertEquals(source.indexOf("import function " + impdescr.getTarget()), impdescr.getStartCharacter());
    assertEquals(source.indexOf("import function " + impdescr.getTarget()) + ("import function " + impdescr.getTarget()).length(), impdescr.getEndCharacter());
}
Also used : ImportDescr(org.drools.drl.ast.descr.ImportDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) AccumulateImportDescr(org.drools.drl.ast.descr.AccumulateImportDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 5 with ImportDescr

use of org.drools.drl.ast.descr.ImportDescr in project drools by kiegroup.

the class PackageDescrVisitor method visitImports.

private void visitImports(List<ImportDescr> importDescrs) throws IOException, ClassNotFoundException {
    HashSet<String> imports = new HashSet<String>();
    for (ImportDescr i : importDescrs) {
        String fullName = i.getTarget();
        String name = fullName.substring(fullName.lastIndexOf(".") + 1);
        imports.add(fullName);
        Import objectImport = new Import(i, rulePackage);
        objectImport.setName(fullName);
        objectImport.setShortName(name);
        data.add(objectImport);
        ObjectType objectType = this.data.getObjectTypeByFullName(fullName);
        if (objectType == null) {
            objectType = new ObjectType(i);
        }
        objectType.setName(name);
        objectType.setFullName(fullName);
        data.add(objectType);
    }
    PackageHeaderLoader packageHeaderLoader = new PackageHeaderLoader(imports, jars);
    for (String factTypeName : packageHeaderLoader.getClassNames()) {
        String name = factTypeName.substring(factTypeName.lastIndexOf(".") + 1);
        Collection<String> fieldNames = packageHeaderLoader.getFieldNames(factTypeName);
        for (String fieldName : fieldNames) {
            ObjectType objectType = this.data.getObjectTypeByObjectTypeNameAndPackageName(name, rulePackage.getName());
            Field field = data.getFieldByObjectTypeAndFieldName(objectType.getFullName(), fieldName);
            if (field == null) {
                field = ObjectTypeFactory.createField(objectType.getDescr(), fieldName, objectType);
                field.setFieldType(packageHeaderLoader.getFieldType(objectType.getName(), fieldName));
                data.add(field);
            }
        }
    }
}
Also used : PackageHeaderLoader(org.drools.verifier.jarloader.PackageHeaderLoader) ObjectType(org.drools.verifier.components.ObjectType) Field(org.drools.verifier.components.Field) Import(org.drools.verifier.components.Import) ImportDescr(org.drools.drl.ast.descr.ImportDescr) HashSet(java.util.HashSet)

Aggregations

ImportDescr (org.drools.drl.ast.descr.ImportDescr)20 FunctionImportDescr (org.drools.drl.ast.descr.FunctionImportDescr)11 PackageDescr (org.drools.drl.ast.descr.PackageDescr)9 AccumulateImportDescr (org.drools.drl.ast.descr.AccumulateImportDescr)8 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)5 Test (org.junit.Test)5 HashSet (java.util.HashSet)4 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)4 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)4 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)4 AbstractClassTypeDeclarationDescr (org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr)4 AttributeDescr (org.drools.drl.ast.descr.AttributeDescr)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 EnumDeclarationDescr (org.drools.drl.ast.descr.EnumDeclarationDescr)2 RuleDescr (org.drools.drl.ast.descr.RuleDescr)2 TypeDeclarationDescr (org.drools.drl.ast.descr.TypeDeclarationDescr)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1