Skip to content

Commit

Permalink
eclipse-archived#190 In progress TqCL - syntax (insert) and library s…
Browse files Browse the repository at this point in the history
…ervices
  • Loading branch information
rtotaro committed Jan 7, 2017
1 parent 95209f0 commit 0fc0001
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public ModelBuilderService getModelBuilderService(Class<?> modelClass) {
return null;
}

public TqCLLibraryProvider getTcqlLibraryProvider()
public TqCLLibraryProvider getTqclLibraryProvider()
{
ServiceReference<TcQLLibraryProviderProxy> serviceReference = getContext().getServiceReference(TcQLLibraryProviderProxy.class);
TcQLLibraryProviderProxy service = getContext().getService(serviceReference);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public interface TqCLLibraryProvider {

public EntityDescriptor getActor(String library,String actorClass) throws TqCLLibraryException;

public EntityDescriptor getActor(String actorClass) throws TqCLLibraryException;

public List<DirectorDescriptor> getDirectors(String library) throws TqCLLibraryException;

public List<String> getParameterTypes(String library) throws TqCLLibraryException;
Expand All @@ -33,4 +35,6 @@ public interface TqCLLibraryProvider {

public boolean hasElement(String element);

public boolean hasElementInLibrary(String element, String library, String category);

}
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,30 @@ public void addLibraryProvider(TqCLLibraryProvider tqCLLibraryProvider) {
}

@Override
public EntityDescriptor getActor(String library, String actorName) throws TqCLLibraryException {
return this.libraryProviders.get(library).getActor(library, actorName);
public EntityDescriptor getActor(String library, String actorClass) throws TqCLLibraryException {
return this.libraryProviders.get(library).getActor(library, actorClass);
}

@Override
public EntityDescriptor getActor(String actorClass) throws TqCLLibraryException {
for (TqCLLibraryProvider provider : libraryProviders.values()) {
EntityDescriptor actor = provider.getActor(actorClass);
if(actor!=null)
{
return actor;
}
}
return null;
}

@Override
public boolean hasElementInLibrary(String element, String library,String category) {
for (TqCLLibraryProvider libraryProvider : libraryProviders.values()) {
if (libraryProvider.hasElementInLibrary(element,library,category)) {
return true;
}
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.graphiti.features.ICreateFeature;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.triquetrum.commands.api.TqCLServices;
import org.eclipse.triquetrum.commands.api.services.ActorDescriptor;
Expand All @@ -35,48 +35,28 @@
import org.eclipse.triquetrum.commands.tqcl.Insert;
import org.eclipse.triquetrum.commands.tqcl.Library;
import org.eclipse.triquetrum.commands.tqcl.TriquetrumScript;
import org.eclipse.triquetrum.workflow.editor.TriqDiagramTypeProvider;
import org.eclipse.triquetrum.workflow.editor.TriqFeatureProvider;
import org.eclipse.triquetrum.workflow.editor.features.ModelElementCreateFeature;
import org.eclipse.triquetrum.workflow.model.util.PtolemyUtil;
import org.eclipse.ui.internal.handlers.WizardHandler.New;
import org.eclipse.xtext.Assignment;
import org.eclipse.xtext.RuleCall;
import org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext;
import org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor;

import com.google.common.collect.ArrayTable;
import com.google.common.collect.Multimap;
import com.google.common.collect.Table;

import ptolemy.actor.CompositeActor;
import ptolemy.data.expr.Parameter;
import ptolemy.kernel.Entity;
import ptolemy.kernel.util.Attribute;
import ptolemy.kernel.util.Settable.Visibility;

/**
* See
* https://www.eclipse.org/Xtext/documentation/304_ide_concepts.html#content-assist
* on how to customize the content assistant.
*/
public class TqclProposalProvider extends AbstractTqclProposalProvider {

private Map<String, ModelElementCreateFeature> featuresMap = new HashMap<>();

private Map<String, EntityDescriptor> entityDescriptorsMap = new HashMap<>();

private void initFeatureMap() {
if (featuresMap.isEmpty()) {
TriqFeatureProvider featureProvider = new TriqFeatureProvider(new TriqDiagramTypeProvider());
ICreateFeature[] createFeatures = featureProvider.getCreateFeatures();
for (ICreateFeature feature : createFeatures) {
if (feature instanceof ModelElementCreateFeature) {
ModelElementCreateFeature modelfeature = (ModelElementCreateFeature) feature;
String key = modelfeature.getGroup() + "." + modelfeature.getName();
featuresMap.put(key, modelfeature);
}
}

@Override
public void completeLibrary_Name(EObject model, Assignment assignment, ContentAssistContext context,
ICompletionProposalAcceptor acceptor) {
super.completeLibrary_Name(model, assignment, context, acceptor);
TqCLLibraryProvider tqclLibraryProvider = TqCLServices.getInstance().getTqclLibraryProvider();
Set<String> libraryNames = tqclLibraryProvider.getLibraryNames();
for (String string : libraryNames) {
ICompletionProposal completionProposal = createCompletionProposal(string,
string, null, context);
acceptor.accept(completionProposal);
}
}

Expand All @@ -85,14 +65,13 @@ private void initFeatureMap() {
public void completeInsert_EntityClass(EObject model, Assignment assignment, ContentAssistContext context,
ICompletionProposalAcceptor acceptor) {
super.completeInsert_EntityClass(model, assignment, context, acceptor);

Map<String, EntityDescriptor> entityDescriptorsMap = new HashMap<>();
if (model instanceof Insert) {
try {

Insert insert = (Insert) model;
TriquetrumScript triquetrumScript = (TriquetrumScript) insert.eContainer();
EList<Library> libraries = triquetrumScript.getLibraries();
TqCLLibraryProvider tcqlLibraryProvider = TqCLServices.getInstance().getTcqlLibraryProvider();
Category category = insert.getCategory();
List<? extends EntityDescriptor> descriptors = new ArrayList<>();
if (libraries == null) {
Expand All @@ -101,6 +80,7 @@ public void completeInsert_EntityClass(EObject model, Assignment assignment, Con
if (insert.getCategory() == null) {
category = Category.ACTOR;
}
TqCLLibraryProvider tcqlLibraryProvider = TqCLServices.getInstance().getTqclLibraryProvider();
for (Library library : libraries) {
switch (category) {
case ACTOR:
Expand Down Expand Up @@ -162,39 +142,39 @@ public void completeInsert_Parameters(EObject model, Assignment assignment, Cont
}
String entityClass = insert.getEntityClass();

EntityDescriptor actor = entityDescriptorsMap.get(entityClass.substring(1, entityClass.lastIndexOf('>')));
if (actor == null) {
return;
}
List<ParameterDescriptor> parameters = actor.getParameters();
for (ParameterDescriptor parameterDescriptor : parameters) {
String displayName = parameterDescriptor.getDisplayName();
String proposal = "";
if (displayName.contains(" ")) {
proposal = "\"" + displayName + "\"";
} else {
proposal = MessageFormat.format("${0}=\"{1}\"", displayName, parameterDescriptor.getDefaultValue());
EntityDescriptor actor;
try {
actor = TqCLServices.getInstance().getTqclLibraryProvider().getActor(cleanEntityName(entityClass));

if (actor == null) {
return;
}
String description = displayName + "(default=" + parameterDescriptor.getDefaultValue() + ")";
List<ParameterDescriptor> parameters = actor.getParameters();
for (ParameterDescriptor parameterDescriptor : parameters) {
String displayName = parameterDescriptor.getDisplayName();
String proposal = "";
if (displayName.contains(" ")) {
proposal = "\"" + displayName + "\"";
} else {
proposal = MessageFormat.format("${0}=\"{1}\"", displayName,
parameterDescriptor.getDefaultValue());
}
String description = displayName + "(default=" + parameterDescriptor.getDefaultValue() + ")";

ICompletionProposal completionProposal = createCompletionProposal(proposal, description, null, context);
acceptor.accept(completionProposal);
ICompletionProposal completionProposal = createCompletionProposal(proposal, description, null,
context);
acceptor.accept(completionProposal);
}
} catch (TqCLLibraryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

@Override
public void complete_Parameter(EObject model, RuleCall ruleCall, ContentAssistContext context,
ICompletionProposalAcceptor acceptor) {

if (model instanceof org.eclipse.triquetrum.commands.tqcl.Parameter) {
initFeatureMap();
org.eclipse.triquetrum.commands.tqcl.Parameter param = (org.eclipse.triquetrum.commands.tqcl.Parameter) model;
EObject eContainer = param.eContainer();

}

public String cleanEntityName(String entityClass) {
return entityClass.substring(1, entityClass.lastIndexOf('>'));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,116 @@
*/
package org.eclipse.triquetrum.commands.validation;

import java.text.MessageFormat;
import java.util.List;
import java.util.Set;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.triquetrum.commands.api.TqCLServices;
import org.eclipse.triquetrum.commands.api.services.EntityDescriptor;
import org.eclipse.triquetrum.commands.api.services.ParameterDescriptor;
import org.eclipse.triquetrum.commands.api.services.TqCLLibraryException;
import org.eclipse.triquetrum.commands.api.services.TqCLLibraryProvider;
import org.eclipse.triquetrum.commands.tqcl.Insert;
import org.eclipse.triquetrum.commands.tqcl.Library;
import org.eclipse.triquetrum.commands.tqcl.Parameter;
import org.eclipse.triquetrum.commands.tqcl.TqclPackage;
import org.eclipse.xtext.validation.Check;

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

// public static final INVALID_NAME = 'invalidName'
//
// @Check
// public void checkGreetingStartsWithCapital(Greeting greeting) {
// if (!Character.isUpperCase(greeting.getName().charAt(0))) {
// warning("Name should start with a capital",
// TqclPackage.Literals.GREETING__NAME,
// INVALID_NAME);
// }
// }


public static final String INVALID_ENTITY_CLASS = "invalidEntityClass";

public static final String INVALID_LIBRARY = "invalidLibrary";

public static final String INVALID_PARAMETER = "invalidParameter";

@Check
public void checkLibrary(Library library) {
String name = library.getName();
Set<String> libraryNames = TqCLServices.getInstance().getTqclLibraryProvider().getLibraryNames();
if (!libraryNames.contains(name)) {
warning("Invalid Library Name", TqclPackage.Literals.LIBRARY__NAME, INVALID_LIBRARY);
}
}

@Check
public void checkEntityInLibrary(Insert insert) {
String entityClass = cleanEntityClassName(insert.getEntityClass());
EList<EObject> eContents = insert.eContainer().eContents();
for (EObject eObject : eContents) {
if (eObject instanceof Library) {
Library library = (Library) eObject;
String libraryName = library.getName();
TqCLLibraryProvider tqclLibraryProvider = TqCLServices.getInstance().getTqclLibraryProvider();
if (tqclLibraryProvider.hasElementInLibrary(entityClass, libraryName, insert.getCategory().getName())) {
return;
}

}
}
String message = MessageFormat.format("Entity class {0} not found in imported library", entityClass);
error(message, TqclPackage.Literals.INSERT__ENTITY_CLASS, INVALID_ENTITY_CLASS);
}

private String cleanEntityClassName(String entityClass) {
return entityClass.substring(1, entityClass.lastIndexOf('>'));
}

@Check
public void checkEntityParameters(Insert insert) {
TqCLLibraryProvider tqclLibraryProvider = TqCLServices.getInstance().getTqclLibraryProvider();
EList<EObject> eContents = insert.eContainer().eContents();
for (EObject eObject : eContents) {
if (eObject instanceof Library) {
Library library = (Library) eObject;
String libraryName = library.getName();
try {
EntityDescriptor actor = tqclLibraryProvider.getActor(libraryName,
cleanEntityClassName(insert.getEntityClass()));
if (actor != null) {
List<ParameterDescriptor> parameters = actor.getParameters();
EList<Parameter> tqclParameters = insert.getParameters();
for (Parameter tqclParameter : tqclParameters) {
boolean found = false;
for (ParameterDescriptor parameterDescriptor : parameters) {
if (parameterDescriptor.getDisplayName()
.equals(cleanParameterName(tqclParameter.getId()))) {
found = true;
break;
}
}
if (!found) {
String message = MessageFormat.format("Invalid paramete name {0} for actor {1}",
tqclParameter.getId(), actor.getClazz());
error(message, tqclParameter, TqclPackage.Literals.PARAMETER__ID, INVALID_PARAMETER);
}
}
}
} catch (TqCLLibraryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

private Object cleanParameterName(String id) {
if (id.startsWith("$")) {
return id.substring(1);
} else if (id.startsWith("\"")) {
return id.substring(1, id.lastIndexOf('"'));
}

// TODO : throw exception???
return null;
}

}
Loading

0 comments on commit 0fc0001

Please sign in to comment.