Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Using Xtext DSL as a rule engine on runtime
Using Xtext DSL as a rule engine on runtime [message #1821429] Wed, 12 February 2020 22:26 Go to next message
Ihar Valnou is currently offline Ihar ValnouFriend
Messages: 1
Registered: February 2020
Junior Member
Dear Xtext experts,
Thanks a lot for such an awesome technology and tools built around it!
We are using an Xtext technology to build a custom DSL which allow us to use different conditions as part of a rule engine and get a result of such conditions by running a DSL interpreter on runtime.
Example of such condition is here:
if(doSomething()) {
     return  true;
} else {
     return false;
}


To run an interpreter we just create a model from our string conditions, load it as resource, get model and run interpreter.
Here it's a way we create and load our resource with a condition string:
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
resourceSet.createResource(URI.createURI(UUID.randomUUID().toString());
InputStream inputStream = new ByteArrayInputStream("condition as string");
resource.load(inputStream, resourceSet.getLoadOptions());

Could you help me with the following questions:
1. Do we use a ResourceSet in a proper way? Maybe some pieces of advice?
2. Do you see any possible issues (out of memory, timings etc)

Thanks in advance!
Re: Using Xtext DSL as a rule engine on runtime [message #1821568 is a reply to message #1821429] Sat, 15 February 2020 07:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13637
Registered: July 2009
Senior Member
hi, here are a some remarks
- the uri should have your file extension.
- you can use Xtexts built-in StringInputStream
- the resolve all is not needed as xtext resolves proxies lazy. it would also not work (as expected) once you ever have more than one resource

- having a look at timeings / performance you should do if you consider to have huge files


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using Xtext DSL as a rule engine on runtime [message #1821705 is a reply to message #1821568] Tue, 18 February 2020 20:54 Go to previous messageGo to next message
Neeraj Bhusare is currently online Neeraj BhusareFriend
Messages: 164
Registered: July 2009
Location: Canada
Senior Member
Adding to Christians comments

> XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
You could just do - XtextResourceSet resourceSet = new XtextResourceSet()

> resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
ECoreUtil#resolveAll() gets called internally, which tries to resolve all the proxies in your model. It might not be a good idea to load the complete model in the memory right at the start, especially if its a deep hierarchy

> esourceSet.createResource(URI.createURI(UUID.randomUUID().toString());
The file extension in the URI is required for getting the Resource Factory. It needs to be registered if you are running your application in standalone mode

> resource.load(inputStream, resourceSet.getLoadOptions());
resource.load(new StringInputStream("condition as string"));

Hope this helps.



Twitter : @NeerajBhusare
Blog : https://nbhusare.github.io/
Best regards, Neeraj
Re: Using Xtext DSL as a rule engine on runtime [message #1821706 is a reply to message #1821568] Tue, 18 February 2020 20:55 Go to previous message
Neeraj Bhusare is currently online Neeraj BhusareFriend
Messages: 164
Registered: July 2009
Location: Canada
Senior Member
Adding to Christians comments

> XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
You could just do - XtextResourceSet resourceSet = new XtextResourceSet()

> resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
ECoreUtil#resolveAll() gets called internally, which tries to resolve all the proxies in your model. It might not be a good idea to load the complete model in the memory right at the start, especially if its a deep hierarchy

> esourceSet.createResource(URI.createURI(UUID.randomUUID().toString());
The file extension in the URI is required for getting the Resource Factory. It needs to be registered if you are running your application in standalone mode

> resource.load(inputStream, resourceSet.getLoadOptions());
resource.load(new StringInputStream("condition as string"));

Hope this helps.



Twitter : @NeerajBhusare
Blog : https://nbhusare.github.io/
Best regards, Neeraj
Previous Topic:Xtext and JUnit 5
Next Topic:Null attribute values in cross-reference from other file
Goto Forum:
  


Current Time: Mon Oct 26 08:06:07 GMT 2020

Powered by FUDForum. Page generated in 0.01470 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top