Skip to main content



      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 17:26 Go to next message
Eclipse UserFriend
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 02:43 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: Using Xtext DSL as a rule engine on runtime [message #1821705 is a reply to message #1821568] Tue, 18 February 2020 15:54 Go to previous messageGo to next message
Eclipse UserFriend
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.

Re: Using Xtext DSL as a rule engine on runtime [message #1821706 is a reply to message #1821568] Tue, 18 February 2020 15:55 Go to previous message
Eclipse UserFriend
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.

Previous Topic:Xtext and JUnit 5
Next Topic:Null attribute values in cross-reference from other file
Goto Forum:
  


Current Time: Sun Jul 20 14:24:07 EDT 2025

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

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

Back to the top