Drools : How to remove dynamically created drl? - drools

I am working on a use case where I need to dynamically add/update/remove drls.
Here I am generating drl string using Rule Templates(with the data from a database) and adding it with the below code.
KieServices ks = KieServices.Factory.get();
KieRepository kr = ks.getRepository();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write("src/main/resources/" + ruleName + ".drl", drl);
KieBuilder kb = ks.newKieBuilder(kfs);
kb.buildAll();
if (kb.getResults().hasMessages(Message.Level.ERROR)) {
System.out.println(kb.getResults().toString());
}
KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
Which API I should use for removing these drls?

The KieFileSystem has a delete method that is likely what you need. The write method, which you are already uses, "writes" a DRL file to the file system. The delete method deletes that DRL file. You'll need to specify the file path when you call the method, so it'll be necessary to keep track of those, especially if you have multiple.
Note that the method takes a vararg so you can pass multple files.
KieServices ks = KieServices.Factory.get();
KieRepository kr = ks.getRepository();
KieFileSystem kfs = ks.newKieFileSystem();
// Write the file:
kfs.write("src/main/resources/" + ruleName + ".drl", drl);
// Delete the file:
kfs.delete("src/main/resources/" + ruleName + ".drl");
Source: KieFileSystem javadoc

Related

What is the way to stop double encoding on File Path?

I want to get the file in resource object using UrlResource, but .toUri method encode path in such a way, that there is file exist, but still, it can't get.
My code is:
val filepath = Paths.get(getClass.getClassLoader.getResource("some file4216.pdf").getPath)
val resource : Resource = new UrlResource(filepath.toUri)
val contentType = "application/pdf"
ResponseEntity.ok()
.contentType(MediaType.parseMediaType(contentType))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename + "\"")
.body(resource)
It will encode space as "%20", but then also it performs encoding and convert "%" into "%2520".
And that's why file not found.
Give below path:
/home/user/Live_Projects/some-project/some-project-api/build/resources/main/some%2520file4216.pdf
I want only %20 not %2520.

Sample vcdiff-java client

I am trying out vcdiff for creating a diff file from source and target files.
Also, will be applying the diff on source file to get the target file.
I have achieved the above use case with xdelta linux command line tool.
But how to achieve the same using vcdiff-java APIs ?
Any hints or directions will be useful to get started.
Thanks.
I think basic use case of diff and apply can be handled as follows.
Here, diff is generated from source.txt and target.txt. Then, diff is applied on source.txt to get result.txt which is equal to target.txt.
Path source = Files.createFile(Paths.get(basePath + "source.txt"));
Files.write(source, new StringBuilder(
"First line of the file.\n"
+ "Second line of the file."
).toString().getBytes());
Path target = Files.createFile(Paths.get(basePath + "target.txt"));
Files.write(target, "1. First line of the file!".getBytes());
final ByteArrayOutputStream delta_ = new ByteArrayOutputStream();
VCDiffEncoder<OutputStream> encoder = VCDiffEncoderBuilder.builder()
.withDictionary(Files.readAllBytes(source))
.withTargetMatches(false)
.withChecksum(true)
.withInterleaving(true)
.buildSimple();
encoder.encode(Files.readAllBytes(target), delta_);
ByteArrayOutputStream result_out = new ByteArrayOutputStream();
VCDiffDecoder decoder = VCDiffDecoderBuilder.builder().buildSimple();
decoder.decode(Files.readAllBytes(source), delta_.toByteArray(), result_out);
Path result = Files.createFile(Paths.get(basePath + "result.txt"));
Files.write(result, result_out.toByteArray());

Read Forms from BAR file - Flowable in code -

I need an example of usign FormEngine. To be more especific....
I'm executing code below - but there’s no forms found in my BAR file :(
The BAR file was exported from Flowable Modeler and it contains one form and one process and app. Maybe there's other way to deploy and obtain forms...?
RepositoryService repositoryService = processEngine.getRepositoryService();
FormRepositoryService formRepositoryService = formEngine.getFormRepositoryService();
File file = new File(path);
ZipInputStream inputStream = new ZipInputStream(new FileInputStream(path));
String idDeployParent = repositoryService.createDeployment()
.name(file.getName())
.addZipInputStream(inputStream)
.deploy()
.getId();
DeploymentEntity deploymentEntity = (DeploymentEntity) repositoryService.createDeploymentQuery().list().get(0);
formRepositoryService.createDeployment()
.name(file.getName())
.parentDeploymentId(idDeployParent)
.deploy();
System.out.println(" FORMS FOUND: " + formRepositoryService.createFormDefinitionQuery().list().size());

Typesafe Config does not take System properties that were set in runtime

I have a core library that sets its configuration as system properties.
mail.from = mail#example.com
I wanted to override my config with them, like:
my.mail.from = "me#example.com"
my.mail.from = ${?mail.from}
This works in unit tests. In my Play (2.6) application it does not.
PropertiesConfiguration.init() // this inits the system properties
info("mail.from: " + sys.props.get("mail.from")) // >> 'mail#example.com' as expected
val config = ConfigFactory.load()
info("my.mail.from: " + config.getString("my.mail.from")) // >> 'me#example.com' instead of 'mail#example.com'
Is this not possible or do I miss something?
Try invalidating any cached configurations in order to pick up changes to system properties:
PropertiesConfiguration.init()
info("mail.from: " + sys.props.get("mail.from"))
ConfigFactory.invalidateCaches()
val config = ConfigFactory.load()
info("my.mail.from: " + config.getString("my.mail.from"))

How to load drool file from in-memory?

I have generated the alerts using the drools , i had created the drool files from database.Every time i am loading the .drl file from resource folder in the project because of this one i am doing lot of io operation.
I want overcome from this problem , any way is there to load drool file from in-memory same as cashing?
That's Drools 5? Time to upgrade. But you just use the same steps as I've provided them using code for Drools 6; the class names have changed somewhat. You do this once:
KieBase kieBase = ...;
FileOutputStream fos1 = new FileOutputStream( OUTPATH );
ObjectOutputStream oos1 = new ObjectOutputStream( fos1 );
oos1.writeObject( kieBase );
oos1.close();
You do this for each run:
FileInputStream fis9 = new FileInputStream( OUTPATH );
ObjectInputStream ois9 = new ObjectInputStream( fis9 );
KieBase kieBase1 = (KieBase)ois9.readObject();
KieSession kieSession = kieBase1.newKieSession();