Skip to main content



      Home
Home » Modeling » TMF (Xtext) » How to get index of the variable in the text of DSL program?
How to get index of the variable in the text of DSL program? [message #1719605] Sat, 09 January 2016 14:17 Go to next message
Eclipse UserFriend
Hi, all.
I need to create varning in my AbstractCasualIntellectValidator. I use for it method

AbstractCasualIntellectValidator.warning(String message, EObject source, EStructuralFeature feature, int index)


As I understand int index is index of the start of wrong code construction in the program of my DSL language.

Is it true?
If yes, how I can to get mentonied index. If not, how I can hilight wrong construction?

Regards
Re: How to get index of the variable in the text of DSL program? [message #1719617 is a reply to message #1719605] Sun, 10 January 2016 02:11 Go to previous messageGo to next message
Eclipse UserFriend
hi,

if this is a question on xtext the index is ususally the index of a Child in a Parents list

e.g.

Parent:
childs+=Child*
;

Child:
name=ID
;

then the following methods may help you to calculate the index

generically:
org.eclipse.emf.ecore.EObject.eContainer()
org.eclipse.emf.ecore.EObject.eContainingFeature()
org.eclipse.emf.ecore.EObject.eGet(EStructuralFeature)
(+ Cast to list and index of)
or you use:
org.eclipse.emf.ecore.EObject.eContainer() + cast to Parent
Parent.getChild().indexOf(c)
Re: How to get index of the variable in the text of DSL program? [message #1719635 is a reply to message #1719617] Sun, 10 January 2016 15:10 Go to previous messageGo to next message
Eclipse UserFriend
I have following realisation of the validator

/*
 * generated by Xtext 2.9.1
 */
package org.casualintellect.validation

import java.util.LinkedList
import java.util.List
import org.casualintellect.casualIntellect.CasualIntellectPackage
import org.casualintellect.casualIntellect.Model
import org.casualintellect.casualIntellect.Transition
import org.eclipse.emf.ecore.EObject
import org.eclipse.xtext.validation.Check

/**
 * This class contains custom validation rules. 
 * 
 * See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#validation
 */
class CasualIntellectValidator extends AbstractCasualIntellectValidator {

	@Check
	def checkTransitions(Model model) {
		var states = model.list_of_states;
		val listOfStateNames = new LinkedList<String>();
		for (var i = 0; i < states.length; i++) {
			var state = states.get(i);
			listOfStateNames.add(state.name);
		}

		for (var i = 0; i < states.length; i++) {
			val state = states.get(i);
			val name=state.name;
			var list = listOfStateNames.filter[equals(name)]

			if (list.size > 1) {
				//val index=foundIndex(state)
				warning('There are several states with the same name:' + name, CasualIntellectPackage.Literals.STATE,
					CasualIntellectPackage::eINSTANCE.state_Name)
			}

		}
		
		for (var i = 0; i < states.length; i++) {
			val state=states.get(i);
			val transitionsList=state.transitions.list;
			
				transitionsList.forEach[transition|checkTransition(transition,listOfStateNames)]
				
			
			
		}
	}
	
	def checkTransition(Transition transition,List<String> stateNamesList){
		if (!stateNamesList.contains(transition.name)){
			val index=foundIndex(transition);
			warning('No state for transition ' + transition.name, CasualIntellectPackage.Literals.STATE,
					CasualIntellectPackage::eINSTANCE.state_Name,index)
		}
	}
	
	def foundIndex(EObject object){
		val container=object.eContainer;		
		var index=container.eContents.indexOf(object);		
		index;
	}

}


For validation following DSL program
state state1{
			before: salo, pivo, vodka;
		    after:salo,pivo,vodka; 
		    in_process:pivo, vodka, salo; 
		    transitions : {transition:salo; condition:true; methods:salo,pivo,vodka} 
		  }

Method foundIndex return 0, and I have following exception. Where I'm wrong?
java.lang.IllegalArgumentException: You can only add issues for EObjects contained in the currently validated resource 'platform:/resource/test/test.casual'. But the given EObject was contained in 'http://www.casualintellect.org/CasualIntellect'
	at org.eclipse.xtext.validation.AbstractDeclarativeValidator.checkIsFromCurrentlyCheckedResource(AbstractDeclarativeValidator.java:571)
	at org.eclipse.xtext.validation.AbstractDeclarativeValidator.acceptWarning(AbstractDeclarativeValidator.java:580)
	at org.eclipse.xtext.validation.AbstractDeclarativeValidator.warning(AbstractDeclarativeValidator.java:399)
	at org.eclipse.xtext.validation.AbstractDeclarativeValidator.warning(AbstractDeclarativeValidator.java:394)
	at org.casualintellect.validation.CasualIntellectValidator.checkTransition(CasualIntellectValidator.java:78)
	at org.casualintellect.validation.CasualIntellectValidator.lambda$1(CasualIntellectValidator.java:62)
	at org.casualintellect.validation.CasualIntellectValidator$$Lambda$4/2009006352.accept(Unknown Source)
	at java.lang.Iterable.forEach(Unknown Source)
	at org.casualintellect.validation.CasualIntellectValidator.checkTransitions(CasualIntellectValidator.java:64)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.eclipse.xtext.validation.AbstractDeclarativeValidator$MethodWrapper.invoke(AbstractDeclarativeValidator.java:118)
	at org.eclipse.xtext.validation.AbstractDeclarativeValidator.internalValidate(AbstractDeclarativeValidator.java:312)
	at org.eclipse.xtext.validation.AbstractInjectableValidator.validate(AbstractInjectableValidator.java:71)
	at org.eclipse.xtext.validation.CompositeEValidator.validate(CompositeEValidator.java:151)
	at org.eclipse.emf.ecore.util.Diagnostician.doValidate(Diagnostician.java:171)
	at org.eclipse.emf.ecore.util.Diagnostician.validate(Diagnostician.java:158)
	at org.eclipse.emf.ecore.util.Diagnostician.validate(Diagnostician.java:137)
	at org.eclipse.xtext.validation.CancelableDiagnostician.validate(CancelableDiagnostician.java:39)
	at org.eclipse.emf.ecore.util.Diagnostician.validate(Diagnostician.java:120)
	at org.eclipse.xtext.validation.ResourceValidatorImpl.validate(ResourceValidatorImpl.java:146)
	at org.eclipse.xtext.validation.ResourceValidatorImpl.validate(ResourceValidatorImpl.java:124)
	at org.eclipse.xtext.validation.ResourceValidatorImpl.validate(ResourceValidatorImpl.java:90)
	at org.eclipse.xtext.ui.validation.DefaultResourceUIValidatorExtension.addMarkers(DefaultResourceUIValidatorExtension.java:60)
	at org.eclipse.xtext.ui.validation.DefaultResourceUIValidatorExtension.updateValidationMarkers(DefaultResourceUIValidatorExtension.java:46)
	at org.eclipse.xtext.builder.builderState.MarkerUpdaterImpl.processDelta(MarkerUpdaterImpl.java:93)
	at org.eclipse.xtext.builder.builderState.MarkerUpdaterImpl.updateMarkers(MarkerUpdaterImpl.java:63)
	at org.eclipse.xtext.builder.builderState.AbstractBuilderState.updateMarkers(AbstractBuilderState.java:82)
	at org.eclipse.xtext.builder.clustering.ClusteringBuilderState.doUpdate(ClusteringBuilderState.java:283)
	at org.eclipse.xtext.builder.builderState.AbstractBuilderState.update(AbstractBuilderState.java:116)
	at org.eclipse.xtext.builder.impl.XtextBuilder.doBuild(XtextBuilder.java:287)
	at org.eclipse.xtext.builder.impl.XtextBuilder.incrementalBuild(XtextBuilder.java:267)
	at org.eclipse.xtext.builder.impl.XtextBuilder.build(XtextBuilder.java:161)
	at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:734)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
	at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:206)
	at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:246)
	at org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:299)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
	at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:302)
	at org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:358)
	at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:381)
	at org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:143)
	at org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:241)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)



Regards,
Vladimir
Re: How to get index of the variable in the text of DSL program? [message #1719637 is a reply to message #1719635] Sun, 10 January 2016 15:46 Go to previous messageGo to next message
Eclipse UserFriend
You can pass only eobjects that are in the same resource as the param of the Check Method
The Parameter in the error or warning method is the instance in the model that has the issue and not its eclass/a constant

So pass the state as third parameter
Re: How to get index of the variable in the text of DSL program? [message #1719771 is a reply to message #1719637] Mon, 11 January 2016 16:57 Go to previous messageGo to next message
Eclipse UserFriend
Do you mean following?
warning('No state for transition ' + transition.name, CasualIntellectPackage.Literals.STATE,
					CasualIntellectPackage::eINSTANCE.state,index)

But, CasualIntellectPackage::eINSTANCE.state has type EClass, not EAttribute

Re: How to get index of the variable in the text of DSL program? [message #1719791 is a reply to message #1719771] Tue, 12 January 2016 00:50 Go to previous messageGo to next message
Eclipse UserFriend
actually not

 @Check
    public void checkSomeething(State state) {
        if (true /*..*/) {
            warning("SomeMessage", 
            		state,
            		CasualIntellectPackage::eINSTANCE.state_name,
            		index,
            		"IssueCode","Data1","Data2","Data3");
        }
//or
 warning("SomeMessage", 
            		state,
            		CasualIntellectPackage::eINSTANCE.state_name, //not sure about the capitallity of the letters
            		index);
        }
    }
Re: How to get index of the variable in the text of DSL program? [message #1719881 is a reply to message #1719791] Tue, 12 January 2016 15:42 Go to previous messageGo to next message
Eclipse UserFriend
Thank you, it's work.
But, in what way I can highlight wrong piece of code is there some method?
Re: How to get index of the variable in the text of DSL program? [message #1719882 is a reply to message #1719881] Tue, 12 January 2016 15:45 Go to previous messageGo to next message
Eclipse UserFriend
this is what the warning method does ?!? it underlines the wrong piece
Re: How to get index of the variable in the text of DSL program? [message #1719883 is a reply to message #1719882] Tue, 12 January 2016 15:50 Go to previous message
Eclipse UserFriend
p.s: if you ask on how to change from "underline" to something else

Settings -> General -> Editor -> Text Editor -> Annotations
Previous Topic:ImportUriGlobalScopeProvider: bug?
Next Topic:Broken cross-references to "dynamic instance" objects in XMI resources
Goto Forum:
  


Current Time: Sun Jul 13 12:55:51 EDT 2025

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

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

Back to the top