Home » Modeling » TMF (Xtext) » Content assist question (an other one ...)
|
Re: Content assist question [message #719019 is a reply to message #718940] |
Thu, 25 August 2011 19:50 |
|
Hi,
what about simplyfing the grammar and solve the enum problem with a check + content assist?
the main promlem is that enums are pain in the ass in this relation
PlanItem:
itemRef = [Entity|ID] (commCalcType = CommInPlanType | planCalcType = PlanInPlanType)
'[' (values+= Value )+ ']' ;
enum CommInPlanType:
CommInPlanTypeNull | cons = 'cons'| prod = 'prod' | avail = 'avail' ;
enum PlanInPlanType:
PlanInPlanTypeNull | annual = "annual" | phastot = "phastot"
| phasadd = "phasadd" | phasold = "phasold" ;
public class MyDslProposalProvider extends AbstractMyDslProposalProvider {
@Override
public void completeKeyword(Keyword keyword,
ContentAssistContext context,
ICompletionProposalAcceptor acceptor) {
PlanItem item = (PlanItem) context.getCurrentModel();
EEnum eenum = getEEnum(keyword);
if (keyword.getValue().equals("CommInPlanTypeNull") || keyword.getValue().equals("PlanInPlanTypeNull")) {
return;
}
if (eenum == null) {
super.completeKeyword(keyword, context, acceptor);
} else {
if (item.getItemRef() instanceof Commodity) {
if (MyDslPackage.Literals.COMM_IN_PLAN_TYPE.equals(eenum)) {
super.completeKeyword(keyword, context, acceptor);
}
} else if (item.getItemRef() instanceof Commodity) {
if (MyDslPackage.Literals.PLAN_IN_PLAN_TYPE.equals(eenum)) {
super.completeKeyword(keyword, context, acceptor);
}
}
}
}
EEnum getEEnum(Keyword keyword) {
if (keyword.eContainer() instanceof EnumLiteralDeclaration) {
return ((EnumLiteralDeclaration)keyword.eContainer()).getEnumLiteral().getEEnum();
}
return null;
}
}
public class MyDslJavaValidator extends AbstractMyDslJavaValidator {
@Check
public void checkPlanItem(PlanItem item) {
if (CommInPlanType.COMM_IN_PLAN_TYPE_NULL.equals(item.getCommCalcType()) && PlanInPlanType.PLAN_IN_PLAN_TYPE_NULL.equals(item.getPlanCalcType())) {
error("please specify a void type", MyDslPackage.Literals.PLAN_ITEM__ITEM_REF);
}
if (item.getItemRef() instanceof Plan) {
if (!CommInPlanType.COMM_IN_PLAN_TYPE_NULL.equals(item.getCommCalcType())) {
error("keyword " + item.getCommCalcType().getLiteral() + " is not allowed here", MyDslPackage.Literals.PLAN_ITEM__COMM_CALC_TYPE);
}
}
if (item.getItemRef() instanceof Commodity) {
if (!PlanInPlanType.PLAN_IN_PLAN_TYPE_NULL.equals(item.getPlanCalcType())) {
error("keyword " + item.getPlanCalcType().getLiteral() + " is not allowed here", MyDslPackage.Literals.PLAN_ITEM__PLAN_CALC_TYPE);
}
}
}
}
~Christian
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
|
|
| |
Re: Content assist question [message #719479 is a reply to message #719024] |
Sat, 27 August 2011 12:30 |
Michel Simeon Messages: 130 Registered: December 2009 |
Senior Member |
|
|
Hi Christian.
I am making progress and give some details for the benefit of others that might be interested (the full software, called Mads, is at http://mads.sourceforge.net/). I still have one question.
The full grammar includes five types of Entity, each with a unique ID:
Entity:
Commodity | Plan | Script | Herd | Table;
A plan item can be any entity except Table:
PlanItem :
itemRef = [Entity|ID]
( (commCalcType = CommInPlanType '[' (values+= Value )+ ']' ';') // commodity in plan
| (planCalcType = PlanInPlanType '[' (values+= Value )+ ']' ';') // plan in plan
| ('[' (values+= Value )+ ']' ';') // herd in plan
| ';' // script in plan
);
Only Plan and Commodity have a result-type attribute and a series-of-values attribute, Herd has values, Script has no attribute.
The javaValidator and proposalProvider, derived from your message, are thus more complicated:
public class MyDslProposalProvider extends AbstractMyDslProposalProvider {
@Override
public void completeKeyword(Keyword keyword,
ContentAssistContext context,
ICompletionProposalAcceptor acceptor) {
EObject currItem = context.getCurrentModel();
if (currItem instanceof PlanItem) {
String proposal;
PlanItem item = (PlanItem) currItem;
// PlanItem item = (PlanItem) context.getCurrentModel();
EEnum eenum = getEEnum(keyword);
if (keyword.getValue().equals("CommInPlanTypeNull")
|| keyword.getValue().equals("PlanInPlanTypeNull")) {
return;
}
if (eenum == null) {
super.completeKeyword(keyword, context, acceptor);
} else {
if (item.getItemRef() instanceof Commodity) {
if (MyDslPackage.Literals.COMM_IN_PLAN_TYPE.equals(eenum)) {
super.completeKeyword(keyword, context, acceptor);
}
} else if (item.getItemRef() instanceof Plan) {
if (MyDslPackage.Literals.PLAN_IN_PLAN_TYPE.equals(eenum)) {
super.completeKeyword(keyword, context, acceptor);
}
}
}
if (item.getItemRef() instanceof Script) proposal = "script item = ID ; ";
else if (item.getItemRef() instanceof Herd) proposal = "herd item = ID [ series of values ] ; ";
else proposal = "item = ID resType [ series of values ] ; ";
proposal = getValueConverter().toString(proposal, "STRING");
ICompletionProposal completionProposal =
createCompletionProposal(proposal, context);
acceptor.accept(completionProposal);
} else if (currItem instanceof Model) {
super.completeKeyword(keyword, context, acceptor);
}
}
....
}
public class MyDslJavaValidator extends AbstractMyDslJavaValidator {
@Check
public void checkPlanItem(PlanItem item) {
if (item.getItemRef() instanceof Plan) {
if (!CommInPlanType.COMM_IN_PLAN_TYPE_NULL.equals(item.getCommCalcType())) {
error("keyword " + item.getCommCalcType().getLiteral() + " is not allowed here", MyDslPackage.Literals.PLAN_ITEM__COMM_CALC_TYPE);
}
if (PlanInPlanType.PLAN_IN_PLAN_TYPE_NULL.equals(item.getPlanCalcType())) {
error("Calculation type keyword is required for Plan Item", MyDslPackage.Literals.PLAN_ITEM__PLAN_CALC_TYPE);
}
// A plan cannot include itself through nested plans
Plan planRefPlan = (Plan) item.getItemRef();
Plan container = (Plan) item.eContainer(); // containing plan
String containerName = container.getName();
Boolean found = false;
found = scanPlan( planRefPlan, containerName, found);
if (found) {
error("A plan cannot include itself through nested plans",
MyDslPackage.Literals.PLAN_ITEM__ITEM_REF);
}
}
else if (item.getItemRef() instanceof Commodity) {
if (!PlanInPlanType.PLAN_IN_PLAN_TYPE_NULL.equals(item.getPlanCalcType())) {
error("keyword " + item.getPlanCalcType().getLiteral() + " is not allowed here", MyDslPackage.Literals.PLAN_ITEM__PLAN_CALC_TYPE);
}
if (CommInPlanType.COMM_IN_PLAN_TYPE_NULL.equals(item.getCommCalcType())) {
error("Calculation type keyword is required for Commodity Item", MyDslPackage.Literals.PLAN_ITEM__COMM_CALC_TYPE);
}
}
else if ((item.getItemRef() instanceof Herd) || (item.getItemRef() instanceof Script)) {
if (!CommInPlanType.COMM_IN_PLAN_TYPE_NULL.equals(item.getCommCalcType())) {
error("keyword " + item.getCommCalcType().getLiteral() + " is not allowed here", MyDslPackage.Literals.PLAN_ITEM__COMM_CALC_TYPE);
}
if (!PlanInPlanType.PLAN_IN_PLAN_TYPE_NULL.equals(item.getPlanCalcType())) {
error("keyword " + item.getPlanCalcType().getLiteral() + " is not allowed here", MyDslPackage.Literals.PLAN_ITEM__PLAN_CALC_TYPE);
}
if (item.getItemRef() instanceof Script){
if (item.getValues().size()!=0) {
error("Time series of Values is not allowed here", MyDslPackage.Literals.PLAN_ITEM__VALUES);
}
}
}
if (!(item.getItemRef() instanceof Script)){
if (item.getValues().size()==0) {
error("Time series of Values is required here", MyDslPackage.Literals.PLAN_ITEM__VALUES);
}
}
}
private Boolean scanPlan (Plan planRefPlan, String containerName, Boolean found){
// scans items of Plan planRef to see if it includes reference to container
...
return found;
}
}
===============
Now, here is my question:
Given the syntax of PlanItem, I always get ; and [ as part of the list of proposals. Could you help me find out how to control those in the proposals provider.
Thanks again for your assistance.
Michel
|
|
| | |
Goto Forum:
Current Time: Thu Oct 17 01:18:36 GMT 2024
Powered by FUDForum. Page generated in 0.03542 seconds
|