Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » how to resolve import files when using in standalone mode(Xtext parser standalone)
icon5.gif  how to resolve import files when using in standalone mode [message #534216] Tue, 18 May 2010 11:51 Go to next message
Yp Li is currently offline Yp LiFriend
Messages: 10
Registered: May 2010
Junior Member
hi,
I am new to XText. I try to use the parser in standalone mode, however, it do not resole imported files automatically, how to make the parser to parser the imported files?
using the Xtext entity demo as an example:
// grammar
Model :
(imports+=Import)*
(elements+=Type)*;

Import :
'import' importURI=STRING;

Type:
SimpleType | Entity;

SimpleType:
'type' name=ID;

Entity :
'entity' name=ID ('extends' extends=[Entity])? '{'
properties+=Property*
'}';
......


DSL files:

// file a.dsl
entity A{}....

// file b.dsl
import "a.dsl"

entity B extends A{}


// standalone code
new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri("../ ");
Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
Resource resource = resourceSet.getResource(
URI.createURI("b.dsl"), true);
Model model = (Model) resource.getContents().get(0);
// it do not resolve a.dsl here and get null pointer error.
Re: how to resolve import files when using in standalone mode [message #534263 is a reply to message #534216] Tue, 18 May 2010 14:11 Go to previous messageGo to next message
Ahmed Qasid is currently offline Ahmed QasidFriend
Messages: 22
Registered: March 2010
Junior Member
Hi Li
Quote:

Resource resource = resourceSet.getResource(
URI.createURI("b.dsl"), true);


use
Resource resource = resourceSet.createResource(URI.createURI("b.dsl"))

instead of getResource
I think getResource searches for the resource in already loaded resources
Best Regards,
Kassyd
Re: how to resolve import files when using in standalone mode [message #534330 is a reply to message #534216] Tue, 18 May 2010 16:54 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi,

your code looks fine. Is "a.dsl" located in the same folder as "b.dsl"?
Do you get any exceptions when loading "b.dsl"?

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 18.05.10 13:51, schrieb Yp Li:
> hi,
> I am new to XText. I try to use the parser in standalone mode, however,
> it do not resole imported files automatically, how to make the parser to
> parser the imported files?
> using the Xtext entity demo as an example:
> // grammar Model :
> (imports+=Import)*
> (elements+=Type)*;
>
> Import :
> 'import' importURI=STRING;
>
> Type:
> SimpleType | Entity;
>
> SimpleType:
> 'type' name=ID;
>
> Entity :
> 'entity' name=ID ('extends' extends=[Entity])? '{'
> properties+=Property*
> '}';
> ......
>
>
> DSL files:
>
> // file a.dsl
> entity A{}....
>
> // file b.dsl
> import "a.dsl"
>
> entity B extends A{}
>
>
> // standalone code
> new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri("../ ");
> Injector injector = new
> MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
> XtextResourceSet resourceSet =
> injector.getInstance(XtextResourceSet.class);
> resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
> Resource resource = resourceSet.getResource(
> URI.createURI("b.dsl"), true);
> Model model = (Model) resource.getContents().get(0);
> // it do not resolve a.dsl here and get null pointer error.
Re: how to resolve import files when using in standalone mode [message #534401 is a reply to message #534263] Wed, 19 May 2010 02:00 Go to previous messageGo to next message
Yp Li is currently offline Yp LiFriend
Messages: 10
Registered: May 2010
Junior Member
hi Kassyd,
thanks for the reply,
when use createResource instead of getResource i got the following exception:

Exception in thread "main" org.eclipse.emf.common.util.BasicEList$BasicIndexOutOfBounds Exception: index=0, size=0
at org.eclipse.emf.common.util.BasicEList.get(BasicEList.java:3 52)
Re: how to resolve import files when using in standalone mode [message #534402 is a reply to message #534330] Wed, 19 May 2010 02:05 Go to previous messageGo to next message
Yp Li is currently offline Yp LiFriend
Messages: 10
Registered: May 2010
Junior Member
hi Sebastian,
thanks for the reply,
both the files are in the same fold, when I load file b.dsl there is no exception,however, all the entities defined in file a.dsl do not exists in the model returned by resource.
Re: how to resolve import files when using in standalone mode [message #534438 is a reply to message #534402] Wed, 19 May 2010 07:49 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Li,

a's entities will never exist as contents in b's model. You may want to
use EcoreUtil.eAllContents(bResource.getResourceSet()) and thereby use
the content of the resource-set.

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 19.05.10 04:05, schrieb Yp Li:
> hi Sebastian,
> thanks for the reply,
> both the files are in the same fold, when I load file b.dsl there is no
> exception,however, all the entities defined in file a.dsl do not exists
> in the model returned by resource.
Re: how to resolve import files when using in standalone mode [message #534489 is a reply to message #534438] Wed, 19 May 2010 10:51 Go to previous messageGo to next message
Yp Li is currently offline Yp LiFriend
Messages: 10
Registered: May 2010
Junior Member
hi Sebastian,
thanks a lot for your reply
maybe I do not make myself clear.
I am going to define an OO search prototype language, there are 3 kinds of elements in my DSL : entity definition, entity data and entity query rules. User could define them in separated files:
// entity.dsl
entity Person{
String name
Int age
}

// data.dsl
import "entity.dsl"
Person(name="Li" age="26")
......

// rule.dsl
import "entity.dsl"
import "data.dsl"
get Person where age > 10

user could use the following command to run it:
java -jar run.jar rule.dsl
our tool need automatically resolve the dependency between the files kind like javac resolve imports.

currently we could only get rules defined in rule.dsl. Does XText could automatically resolve the dependency and return all the elements define in all the 3 files?

Best Regards,
Yp Li
Re: how to resolve import files when using in standalone mode [message #671922 is a reply to message #534489] Thu, 19 May 2011 11:20 Go to previous messageGo to next message
Johan Missing name is currently offline Johan Missing nameFriend
Messages: 3
Registered: May 2011
Junior Member
Dear Yp Li,

I am having the same problem as you and I am not able to get the cross file linking to work in standalone mode. Have you solved the problem? If so, would you care to share the solution?

Best regards,
Johan
Re: how to resolve import files when using in standalone mode [message #671933 is a reply to message #671922] Thu, 19 May 2011 11:53 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
Hello Johan,

can you share a sample grammar, sample models and the java code that reproduces the problem?

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
icon7.gif  Re: how to resolve import files when using in standalone mode [message #672084 is a reply to message #671933] Thu, 19 May 2011 20:28 Go to previous message
Johan Missing name is currently offline Johan Missing nameFriend
Messages: 3
Registered: May 2011
Junior Member
Hi Christian,

Thanks for your quick reply! When cleaning up the code before sending it to you, it started to work. I have no clue why, but while I am not wiser I am at least happier.
I believe the key was in a previous response to a similar question, where it was stated that the resourceset must contain all the file that are referenced.

Anyway, maybe there will be similar questions in the future so I figure I might as well show how my command line class for using xtext as standalone .

The part of the grammar that relates to imports look like this (the origin of the code is ORCC project available at Sourceforge and the language is CAL):

///////////////////////////////////////////////////////////////////////////////
// top-level entity
///////////////////////////////////////////////////////////////////////////////

AstEntity:
('package' package=QualifiedName ';')?
(imports += Import)*

(('actor' name=ID actor=AstActor) | ('unit' name=ID unit=AstUnit));

Import: 'import' importedNamespace=QualifiedNameWithWildCard ';';

QualifiedName:
ID ('.' ID)*;

QualifiedNameWithWildCard:
QualifiedName '.*'?;

====================================

Some sample code is shown below. The function "square" is imported from a separate file "funcs.cal".

File "Add.cal":
------------------------------------------
package SimpleExamples;

import SimpleExamples.funcs.*;


actor Add () int A, int B ==> int Result:

int t = square(2); //<-- THIS WERE THE IMPORTED FUNCTION IS USED.

action A:[a], B:[b] ==> Result:[a + b]
end
end

File "funcs.cal":
------------------------------------------

package SimpleExamples;

unit funcs :
function square(int i) --> int :i*i end //<-- DEFINITION OF FUNCTION TO BE EXPORTED
end

=============================================

Now, to the interesting part (well, well). The command line is expecting a list of files, i.e. something like "FrontendCli *.cal". The main part of this code relates to populating the resourceset with all the files.

public class FrontendCli {

public static void main(String[] args) {
Injector injector = new CalStandaloneSetup().createInjectorAndDoEMFRegistration();
FrontendCli fe = injector.getInstance(FrontendCli.class);
fe.compileActors(args);
}

@Inject
private XtextResourceSet resourceSet;

public void compileActors(String[] args) {
List<String> paths = new ArrayList<String>();
Multimap<String, URI> uris = new HashMultimap<String, URI>();

for (int i = 0; i<args.length; i++) {
File f = new File(args[i]);
paths.add(f.getAbsolutePath());
uris.put(paths.get(i), URI.createFileURI(paths.get(i)));
}
ContainersStateFactory containersStateFactory = new ContainersStateFactory();
IAllContainersState containersState = containersStateFactory.getContainersState(paths, uris);
resourceSet.eAdapters().add(new DelegatingIAllContainerAdapter(containersState));

Collection<URI> values = Sets.newHashSet(uris.values());
for (URI uri : values) {
resourceSet.createResource(uri);
}

boolean hasErrors = false;

Resource resource = (Resource) resourceSet.getResources().get(0);
IResourceValidator v = ((XtextResource) resource).getResourceServiceProvider().getResourceValidator();

List<Resource> resources = Lists.newArrayList(resourceSet.getResources());
for (Resource res : resources) {
List<Diagnostic> errors = res.getErrors();
if (!errors.isEmpty()) {
for (Diagnostic error : errors) {
System.err.println("Diagnostics" + error);
}
hasErrors = true;
}

try {
res.load(null);
List<Issue> result = v.validate(res, CheckMode.ALL, null);
for (Issue issue : result) {
switch (issue.getSeverity()) {
case ERROR:
System.err.println(issue.getMessage());
break;
case WARNING:
System.out.println(issue.getMessage());
break;
case INFO:
System.out.println(issue.getMessage());
break;
}
}
} catch (IOException e) {
System.err.println("Couldn't load resource (" + res.getURI() + ")" + e);
}
}

for (Resource res : resources) {
AstEntity entity = (AstEntity) res.getContents().get(0);
try {
if (!hasErrors) {
String result = new UglyPrinter(entity).compile();
System.out.println(result);
}
} catch (Exception e) {
e.printStackTrace();
}
}

}
}

A few of the steps in populating the resourceSet are more or less copy-past and are possible not the simplest way.

Thanks, again. Sometimes having someone to explain to is all it takes to solve the problems;-)

Ciao,
Johan

Previous Topic:Generating the java object model using XTEXT
Next Topic:Cyclic resolution of lazy links xtext
Goto Forum:
  


Current Time: Fri Sep 20 17:21:26 GMT 2024

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

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

Back to the top