private void buildTypeDeclarations(Collection<CompositePackageDescr> packages) { for (CompositePackageDescr packageDescr : packages) { for (TypeDeclarationDescr typeDeclarationDescr : packageDescr.getTypeDeclarations()) { if (pkgBuilder.isEmpty(typeDeclarationDescr.getNamespace())) { typeDeclarationDescr.setNamespace( packageDescr.getNamespace()); // set the default namespace } pkgBuilder.registerGeneratedType(typeDeclarationDescr); } } List<PackageBuilder.TypeDefinition> unresolvedTypes = new ArrayList<PackageBuilder.TypeDefinition>(); for (CompositePackageDescr packageDescr : packages) { buildTypeDeclarations(packageDescr, unresolvedTypes); } for (PackageBuilder.TypeDefinition unresolvedType : unresolvedTypes) { pkgBuilder.processUnresolvedType( pkgBuilder.getPackageRegistry(unresolvedType.getNamespace()), unresolvedType); } for (CompositePackageDescr packageDescr : packages) { for (ImportDescr importDescr : packageDescr.getImports()) { pkgBuilder.getPackageRegistry(packageDescr.getNamespace()).addImport(importDescr); } } }
private void buildRules(Collection<CompositePackageDescr> packages) { for (CompositePackageDescr packageDescr : packages) { pkgBuilder.setAssetFilter(packageDescr.getFilter()); PackageRegistry pkgRegistry = pkgBuilder.getPackageRegistry(packageDescr.getNamespace()); pkgBuilder.processOtherDeclarations(pkgRegistry, packageDescr); pkgBuilder.compileAllRules(packageDescr, pkgRegistry); pkgBuilder.setAssetFilter(null); } }
@Test public void testQuery() throws Exception { final PackageBuilder builder = new PackageBuilder(); final PackageDescr packageDescr = new PackageDescr("p1"); final QueryDescr queryDescr = new QueryDescr("query1"); queryDescr.addParameter("String", "$type"); packageDescr.addRule(queryDescr); final AndDescr lhs = new AndDescr(); queryDescr.setLhs(lhs); final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton"); lhs.addDescr(pattern); pattern.addConstraint(new ExprConstraintDescr("type == $type")); // Another query, no parameters QueryDescr queryDescr2 = new QueryDescr("query2"); packageDescr.addRule(queryDescr2); AndDescr lhs2 = new AndDescr(); queryDescr2.setLhs(lhs2); PatternDescr pattern2 = new PatternDescr(Cheese.class.getName()); lhs2.addDescr(pattern2); builder.addPackage(packageDescr); assertLength(0, builder.getErrors().getErrors()); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages( Arrays.asList(new KnowledgePackage[] {new KnowledgePackageImp(builder.getPackage())})); final KieSession session = kbase.newStatefulKnowledgeSession(); session.insert(new Cheese("stilton", 15)); QueryResults results = session.getQueryResults("query1", "stilton"); assertEquals(1, results.size()); Object object = results.iterator().next().get("stilton"); assertEquals(new Cheese("stilton", 15), object); results = session.getQueryResults("query1", new Object[] {"cheddar"}); assertEquals(0, results.size()); session.insert(new Cheese("dolcelatte", 20)); results = session.getQueryResults("query2", new Object[] {}); assertEquals(2, results.size()); }
private List<PackageBuilder.TypeDefinition> buildTypeDeclarations( CompositePackageDescr packageDescr, List<PackageBuilder.TypeDefinition> unresolvedTypes) { pkgBuilder.setAssetFilter(packageDescr.getFilter()); PackageRegistry pkgRegistry = pkgBuilder.createPackageRegistry(packageDescr); if (pkgRegistry == null) { return null; } pkgBuilder.processEntryPointDeclarations(pkgRegistry, packageDescr); List<TypeDefinition> processTypeDeclarations = pkgBuilder.processTypeDeclarations(pkgRegistry, packageDescr, unresolvedTypes); pkgBuilder.setAssetFilter(null); return processTypeDeclarations; }
private void buildOthers() { try { for (Map.Entry<ResourceType, List<ResourceDescr>> entry : resourcesByType.entrySet()) { for (ResourceDescr resourceDescr : entry.getValue()) { pkgBuilder.setAssetFilter(resourceDescr.getFilter()); pkgBuilder.addPackageForExternalType( resourceDescr.resource, entry.getKey(), resourceDescr.configuration); pkgBuilder.setAssetFilter(null); } } } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } }
private void registerDSL() { List<ResourceDescr> resourcesByType = this.resourcesByType.remove(ResourceType.DSL); if (resourcesByType != null) { for (ResourceDescr resourceDescr : resourcesByType) { try { pkgBuilder.setAssetFilter(resourceDescr.getFilter()); pkgBuilder.addDsl(resourceDescr.resource); pkgBuilder.setAssetFilter(null); } catch (RuntimeException e) { if (buildException == null) { buildException = e; } } catch (Exception e) { if (buildException == null) { buildException = new RuntimeException(e); } } } } }
public void build() { buildException = null; pkgBuilder.registerBuildResources(getResources()); registerDSL(); buildResources(); buildPackages(); buildOthers(); resourcesByType.clear(); if (buildException != null) { throw buildException; } }
@Test public void testAlternativeSetters() { // given VersionNumber version = version(1, 2, 3, 4); // when Package _package = PackageBuilder.builder().setVersionNumber(version).build(); // then assertThat(_package.getVersionNumber()) .as("Builder should set version number properly") .isNotNull() .isEqualTo(version); }
@Test public void testBuilder() { // given String id = "id"; String name = "some name"; Update update = new Update(); String versionNumber = "12.34.56.78"; SortedSet<ChangelogEntry> changelog = new TreeSet<ChangelogEntry>(Arrays.asList(new ChangelogEntry())); // when Package _package = PackageBuilder.builder() .setChangelog(changelog) .setID(id) .setName(name) .setUpdate(update) .setVersionNumber(versionNumber) .build(); // then assertThat(_package.getChangelog()) .as("Builder should set name properly") .isNotNull() .isEqualTo(changelog); assertThat(_package.getID()).as("Builder should set ID properly").isNotNull().isEqualTo(id); assertThat(_package.getName()) .as("Builder should set name properly") .isNotNull() .isEqualTo(name); assertThat(_package.getUpdate()) .as("Builder should set update properly") .isNotNull() .isEqualTo(update); assertThat(_package.getVersionNumber()) .as("Builder should set version number properly") .isNotNull() .isEqualTo(version(versionNumber)); }
public PackageDescr map(PackageBuilder pkgBuilder, ResourceDescr resourceDescr) throws Exception { return pkgBuilder.scoreCardToPackageDescr( resourceDescr.resource, resourceDescr.configuration); }
public PackageDescr map(PackageBuilder pkgBuilder, ResourceDescr resourceDescr) throws Exception { return pkgBuilder.decisionTableToPackageDescr( resourceDescr.resource, resourceDescr.configuration); }
public PackageDescr map(PackageBuilder pkgBuilder, ResourceDescr resourceDescr) throws Exception { return pkgBuilder.xmlToPackageDescr(resourceDescr.resource); }
private void buildResources() { List<ResourceDescr> resourcesByType = this.resourcesByType.remove(ResourceType.DRF); if (resourcesByType != null) { for (ResourceDescr resourceDescr : resourcesByType) { try { pkgBuilder.setAssetFilter(resourceDescr.getFilter()); pkgBuilder.addProcessFromXml(resourceDescr.resource); } catch (RuntimeException e) { if (buildException == null) { buildException = e; } } catch (Exception e) { if (buildException == null) { buildException = new RuntimeException(e); } } finally { pkgBuilder.setAssetFilter(null); } } } resourcesByType = this.resourcesByType.remove(ResourceType.BPMN2); if (resourcesByType != null) { for (ResourceDescr resourceDescr : resourcesByType) { try { BPMN2ProcessFactory.configurePackageBuilder(pkgBuilder); pkgBuilder.setAssetFilter(resourceDescr.getFilter()); pkgBuilder.addProcessFromXml(resourceDescr.resource); } catch (RuntimeException e) { if (buildException == null) { buildException = e; } } catch (Exception e) { if (buildException == null) { buildException = new RuntimeException(e); } } finally { pkgBuilder.setAssetFilter(null); } } } resourcesByType = this.resourcesByType.remove(ResourceType.PKG); if (resourcesByType != null) { for (ResourceDescr resourceDescr : resourcesByType) { try { pkgBuilder.setAssetFilter(resourceDescr.getFilter()); pkgBuilder.addPackageFromInputStream(resourceDescr.resource); } catch (RuntimeException e) { if (buildException == null) { buildException = e; } } catch (Exception e) { if (buildException == null) { buildException = new RuntimeException(e); } } finally { pkgBuilder.setAssetFilter(null); } } } resourcesByType = this.resourcesByType.remove(ResourceType.CHANGE_SET); if (resourcesByType != null) { for (ResourceDescr resourceDescr : resourcesByType) { try { pkgBuilder.setAssetFilter(resourceDescr.getFilter()); pkgBuilder.addPackageFromChangeSet(resourceDescr.resource); } catch (RuntimeException e) { if (buildException == null) { buildException = e; } } catch (Exception e) { if (buildException == null) { buildException = new RuntimeException(e); } } finally { pkgBuilder.setAssetFilter(null); } } } resourcesByType = this.resourcesByType.remove(ResourceType.XSD); if (resourcesByType != null) { for (ResourceDescr resourceDescr : resourcesByType) { try { pkgBuilder.setAssetFilter(resourceDescr.getFilter()); pkgBuilder.addPackageFromXSD( resourceDescr.resource, (JaxbConfigurationImpl) resourceDescr.configuration); } catch (RuntimeException e) { if (buildException == null) { buildException = e; } } catch (Exception e) { if (buildException == null) { buildException = new RuntimeException(e); } } finally { pkgBuilder.setAssetFilter(null); } } } resourcesByType = this.resourcesByType.remove(ResourceType.PMML); if (resourcesByType != null) { for (ResourceDescr resourceDescr : resourcesByType) { try { pkgBuilder.setAssetFilter(resourceDescr.getFilter()); pkgBuilder.addPackageFromPMML( resourceDescr.resource, ResourceType.PMML, resourceDescr.configuration); } catch (RuntimeException e) { if (buildException == null) { buildException = e; } } catch (Exception e) { if (buildException == null) { buildException = new RuntimeException(e); } } finally { pkgBuilder.setAssetFilter(null); } } } }
@Test public void testBuilder() { // given String packageName = "some package name"; String packageId = "1"; String versionNumber = "23.45.78.00"; String developmentVersion = "true"; String updateId = "2"; String changes = "some changes"; String strategy = "unzip"; String originalName = "name.zip"; String relativePath = "/"; String command = "some command"; Package _package = PackageBuilder.builder().setName(packageName).setVersionNumber("1.0.0.0").build(); File file = new File("/"); // when Update update = UpdateBuilder.builder() .setID(updateId) .setPackageName(packageName) .setPackageID(packageId) .setPackage(_package) .setVersionNumber(versionNumber) .setChanges(changes) .setDevelopmentVersion(developmentVersion) .setUpdateStrategy(strategy) .setOriginalName(originalName) .setRelativePath(relativePath) .setCommand(command) .setFile(file) .build(); // then assertThat(update.getPackageName()) .as("Builder should set package name properly") .isNotNull() .isEqualTo(packageName); assertThat(update.getPackageID()) .as("Builder should set package ID properly") .isNotNull() .isEqualTo(packageId); assertThat(update.getPackage()) .as("Builder should set Package properly") .isNotNull() .isEqualTo(_package); assertThat(update.getVersionNumber()) .as("Builder should set version number properly") .isNotNull() .isEqualTo(version(versionNumber)); assertThat(update.isDevelopmentVersion()) .as("Builder should set development version properly") .isEqualTo(Boolean.valueOf(developmentVersion)); assertThat(update.getID()) .as("Builder should set update ID properly") .isNotNull() .isEqualTo(updateId); assertThat(update.getUpdateStrategy()) .as("Builder should set update strategy properly") .isNotNull() .isEqualTo(Enums.parseMessage(EUpdateStrategy.class, strategy)); assertThat(update.getChanges()) .as("Builder should set changes properly") .isNotNull() .isEqualTo(changes); assertThat(update.getOriginalName()) .as("Builder should set original name properly") .isNotNull() .isEqualTo(originalName); assertThat(update.getRelativePath()) .as("Builder should set relative path properly") .isNotNull() .isEqualTo(""); assertThat(update.getCommand()) .as("Builder should set command properly") .isNotNull() .isEqualTo(command); assertThat(update.getFile()).as("Builder should set file properly").isNotNull().isEqualTo(file); }
@Test public void testRuleWithQuery() throws Exception { final PackageBuilder builder = new PackageBuilder(); final PackageDescr packageDescr = new PackageDescr("p1"); QueryDescr queryDescr = new QueryDescr("query1"); queryDescr.addParameter("String", "$name"); queryDescr.addParameter("int", "$age"); queryDescr.addParameter("String", "$likes"); packageDescr.addRule(queryDescr); AndDescr lhs = new AndDescr(); queryDescr.setLhs(lhs); PatternDescr pattern = new PatternDescr(Person.class.getName()); lhs.addDescr(pattern); pattern.addConstraint(new BindingDescr("$name", "name", true)); pattern.addConstraint(new BindingDescr("$age", "age", true)); pattern.addConstraint(new BindingDescr("$likes", "likes", true)); RuleDescr ruleDescr = new RuleDescr("rule-1"); packageDescr.addRule(ruleDescr); lhs = new AndDescr(); ruleDescr.setLhs(lhs); pattern = new PatternDescr(Cheese.class.getName()); lhs.addDescr(pattern); pattern.addConstraint(new BindingDescr("$type", "type")); pattern = new PatternDescr("query1"); pattern.setQuery(true); lhs.addDescr(pattern); ExprConstraintDescr expr = new ExprConstraintDescr("'bobba'"); expr.setPosition(0); expr.setType(Type.POSITIONAL); pattern.addConstraint(expr); expr = new ExprConstraintDescr("$age"); expr.setPosition(1); expr.setType(Type.POSITIONAL); pattern.addConstraint(expr); expr = new ExprConstraintDescr("$type"); expr.setPosition(2); expr.setType(Type.POSITIONAL); pattern.addConstraint(expr); ruleDescr.setConsequence("System.out.println(\"age: \" + $age);"); builder.addPackage(packageDescr); assertLength(0, builder.getErrors().getErrors()); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages( Arrays.asList(new KnowledgePackage[] {new KnowledgePackageImp(builder.getPackage())})); final KieSession session = kbase.newStatefulKnowledgeSession(); session.insert(new Person("bobba", "stilton", 90)); session.insert(new Person("bobba", "brie", 80)); session.insert(new Person("bobba", "brie", 75)); session.insert(new Person("darth", "brie", 100)); session.insert(new Person("luke", "brie", 25)); session.insert(new Cheese("brie", 25)); session.fireAllRules(); }