Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » XBase: Adding explicit import statement using JvmModelInferrer(Explicit imports using XBase)
XBase: Adding explicit import statement using JvmModelInferrer [message #1739684] Wed, 03 August 2016 19:18 Go to next message
Sonia Ravindran is currently offline Sonia RavindranFriend
Messages: 16
Registered: June 2016
Junior Member
I would like to control the visibility of Entity and Attributes using packages. So use

package com.company.pkg1{
entity Entity1{
ref Entity2
ref Entity3
}
}

package com.company.pkg2{
entity Entity2{
}
}

package com.company.pkg3{
entity Entity2{
}
}

I would like to restrict the visibility and access of Entity3 inside Entity1. So I would like to import only com.company.pkg2 to the generated java class of Entity1, so that only Entity 2 can be accessed.I use JvmModelInferrer to map and generate java class. I could not find the way to explicitly import packages using jvmModelInferer.

Note : I am able to get the visibility of the type using ImportedNamespaceAwareLocalScopeProvider, however need to selectively import a different package for different entity based on the use case at runtime.

Re: XBase: Adding explicit import statement using JvmModelInferrer [message #1739686 is a reply to message #1739684] Wed, 03 August 2016 19:42 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
hi,

am not sure if i get your problem

org.eclipse.xtext.xbase.scoping.XImportSectionNamespaceScopeProvider.internalGetImportedNamespaceResolvers(EObject, boolean)

has an context object


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XBase: Adding explicit import statement using JvmModelInferrer [message #1739696 is a reply to message #1739686] Thu, 04 August 2016 03:31 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Ps can you give more details what you do inside the inferrer

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XBase: Adding explicit import statement using JvmModelInferrer [message #1739818 is a reply to message #1739696] Thu, 04 August 2016 17:49 Go to previous messageGo to next message
Sonia Ravindran is currently offline Sonia RavindranFriend
Messages: 16
Registered: June 2016
Junior Member
Thank you Christian for your quick reply and help.

I have multiple modules, and some global modules.
Each module has some Entities, Attributes(sub-entires) and rules. Actually i am looking to control the visibility and accessibility.For example. the XBockExpression inside a rule should only be able to access only the resources in certain allowed modules and global modules and not any other entities and attributes.

Using inferrer and package concept as shown in Domain model example, i could create separate packages for each modules. However I could not strictly enforce restrictions at module level.

I use an optional XImportSection
importSection=XImportSection?

XImportSection allows to import and access any other package. However, is there a way , where i don't use XImportSelection but i can programmatically import only the required packages through either inferrer or any other way.
Re: XBase: Adding explicit import statement using JvmModelInferrer [message #1739825 is a reply to message #1739818] Thu, 04 August 2016 18:41 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
what do you mean by required package? for the use inside the expression?

package org.xtext.example.mydsl2

import javax.inject.Inject
import org.eclipse.emf.ecore.EObject
import org.eclipse.xtext.EcoreUtil2
import org.eclipse.xtext.naming.IQualifiedNameProvider
import org.eclipse.xtext.xbase.scoping.XImportSectionNamespaceScopeProvider
import org.xtext.example.mydsl2.myDsl.Entity
import org.xtext.example.mydsl2.myDsl.Package

class MyDslXImportSectionNamespaceScopeProvider extends XImportSectionNamespaceScopeProvider {
	
	@Inject IQualifiedNameProvider nameProvider;
	
	override protected internalGetImportedNamespaceResolvers(EObject context, boolean ignoreCase) {
		val result = newArrayList
		result += super.internalGetImportedNamespaceResolvers(context, ignoreCase)
		if (context instanceof Entity) {
			
			val prev = getPrevPack(context)
			if (prev != null) {
				val name = nameProvider.getFullyQualifiedName(prev);
				result += this.createImportedNamespaceResolver(name+".*", ignoreCase)
			}
			
		}
		result
		
	}
	
	def getPrevPack(Entity ctx) {
		val pack = EcoreUtil2.getContainerOfType(ctx, Package)
		val prev = EcoreUtil2.getPreviousSibling(pack)
		if (prev != null && prev instanceof Package) {
			return prev as Package
		}
		return null
		
	}
	
}


class MyDslRuntimeModule extends AbstractMyDslRuntimeModule {
	
	override configureIScopeProviderDelegate(Binder binder) {
		binder.bind(IScopeProvider).annotatedWith(Names.named(AbstractDeclarativeScopeProvider.NAMED_DELEGATE)).to(MyDslXImportSectionNamespaceScopeProvider);
	
	}
	
}


class MyDslJvmModelInferrer extends AbstractModelInferrer {

	@Inject extension JvmTypesBuilder

	@Inject extension IQualifiedNameConverter
	
	def dispatch void infer(Entity entity, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
		// Here you explain how your model is mapped to Java elements, by writing the actual translation code.
		val pack = EcoreUtil2.getContainerOfType(entity, Package)
		val name = pack.name+"."+entity.name
		acceptor.accept(entity.toClass(name.toQualifiedName)) [
			members += entity.toMethod("dummy", Void.TYPE.typeRef()) [
				body = entity.body
			]
		]
	}
}


grammar org.xtext.example.mydsl2.MyDsl with org.eclipse.xtext.xbase.Xbase

generate myDsl "http://www.xtext.org/example/mydsl2/MyDsl"

Model:
	packages+=Package*;
	
Package:
	"package" name=QualifiedName "{"
		enitities+=Entity*
	"}"
;

Entity:
	"entity" name=ID 
	body=XBlockExpression
;


package a {
	entity A {
		
	}
}
package b {
	entity B {
		var A a;
		var C c; // not ok
	}
}

package c {
	entity C {
		var B a; // ok
		var A a; // not ok
	}
}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XBase: Adding explicit import statement using JvmModelInferrer [message #1739827 is a reply to message #1739825] Thu, 04 August 2016 19:10 Go to previous message
Sonia Ravindran is currently offline Sonia RavindranFriend
Messages: 16
Registered: June 2016
Junior Member
Thank You Christian. This is exactly what i was looking for.
Previous Topic:Build project and Build All are greyed out even Build Automatically unselect
Next Topic:How to use text generators and JvmInferrer in one project ?
Goto Forum:
  


Current Time: Thu Apr 25 06:06:12 GMT 2024

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

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

Back to the top