Skip to content

Commit

Permalink
Computes for once the documentation of primitives and their args
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed Feb 19, 2022
1 parent 3c16740 commit 847443e
Showing 1 changed file with 89 additions and 49 deletions.
138 changes: 89 additions & 49 deletions msi.gama.core/src/msi/gaml/descriptions/PrimitiveDescription.java
@@ -1,21 +1,29 @@
/*******************************************************************************************************
*
* PrimitiveDescription.java, in msi.gama.core, is part of the source code of the
* GAMA modeling and simulation platform (v.1.8.2).
* PrimitiveDescription.java, in msi.gama.core, is part of the source code of the GAMA modeling and simulation platform
* (v.1.8.2).
*
* (c) 2007-2022 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
*
********************************************************************************************************/
package msi.gaml.descriptions;

import java.lang.reflect.AccessibleObject;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.ecore.EObject;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;

import msi.gama.common.interfaces.IKeyword;
import msi.gama.precompiler.GamlAnnotations.action;
import msi.gama.precompiler.GamlAnnotations.arg;
import msi.gama.precompiler.GamlAnnotations.doc;
import msi.gaml.compilation.IGamaHelper;
import msi.gaml.operators.Strings;
Expand All @@ -29,21 +37,29 @@ public class PrimitiveDescription extends ActionDescription {

/** The helper. */
private IGamaHelper helper;

/** The method. */
private AccessibleObject method;

/** The plugin. */
private String plugin;

/** The documentation. */
private String documentation;

/**
* Instantiates a new primitive description.
*
* @param superDesc the super desc
* @param source the source
* @param children the children
* @param facets the facets
* @param plugin the plugin
* @param superDesc
* the super desc
* @param source
* the source
* @param children
* the children
* @param facets
* the facets
* @param plugin
* the plugin
*/
public PrimitiveDescription(final IDescription superDesc, final EObject source,
final Iterable<IDescription> children, final Facets facets, final String plugin) {
Expand All @@ -67,9 +83,7 @@ public boolean visitOwnChildren(final DescriptionVisitor visitor) {
}

@Override
public String getDefiningPlugin() {
return plugin;
}
public String getDefiningPlugin() { return plugin; }

@Override
public boolean validateChildren() {
Expand All @@ -78,19 +92,49 @@ public boolean validateChildren() {

@Override
public String getDocumentation() {
String documentation;
if (documentation != null) return documentation;
final doc d = getDocAnnotation();
if (d == null) {
documentation = "";
} else if (d.deprecated().isEmpty()) {
documentation = d.value() + Strings.LN;
} else {
if (d.deprecated().isEmpty()) {
documentation = d.value() + Strings.LN;
} else {
documentation = d.value() + Strings.LN + Strings.LN + d.deprecated() + Strings.LN;
}
documentation = d.value() + Strings.LN + Strings.LN + d.deprecated() + Strings.LN;
}
// Only arguments
return documentation + super.getArgDocumentation();
return documentation + getArgDocumentation();
}

@Override
public String getArgDocumentation() {
if (getArgNames().size() > 0) {
final StringBuilder sb = new StringBuilder(200);
Map<String, arg> argAnnotations = getArgs();
final List<String> args = ImmutableList.copyOf(Iterables.transform(getFormalArgs(), desc -> {

final StringBuilder sb1 = new StringBuilder(100);

String name = desc.getName();
sb1.append("<li><b>").append(Strings.TAB).append(name).append("</b>, type ").append(desc.getGamlType());
if (desc.hasFacet(IKeyword.DEFAULT)) {
sb1.append(" <i>(default: ").append(desc.getFacetExpr(IKeyword.DEFAULT).serialize(false))
.append(")</i>");
}
arg a = argAnnotations.get(name);
if (a != null) {
doc[] d = a.doc();
if (d.length > 0) { sb1.append("; ").append(d[0].value()); }
}
sb1.append("</li>");

return sb1.toString();
}));
sb.append("Arguments accepted: ").append("<br/><ul>");
for (final String a : args) { sb.append(a); }
sb.append("</ul><br/>");
return sb.toString();
}
return "";
}

/**
Expand All @@ -100,9 +144,9 @@ public String getDocumentation() {
*/
public String getDeprecated() {
final doc d = getDocAnnotation();
if (d == null) { return null; }
if (d == null) return null;
String deprecated = d.deprecated();
if (deprecated.isEmpty()) { return null; }
if (deprecated.isEmpty()) return null;
return deprecated;
}

Expand All @@ -115,38 +159,34 @@ public doc getDocAnnotation() {
doc d = null;
if (method != null && method.isAnnotationPresent(doc.class)) {
d = method.getAnnotation(doc.class);
} else {
if (method != null && method.isAnnotationPresent(action.class)) {
final doc[] docs = method.getAnnotation(action.class).doc();
if (docs.length > 0) {
d = docs[0];
}
}
} else if (method != null && method.isAnnotationPresent(action.class)) {
final doc[] docs = method.getAnnotation(action.class).doc();
if (docs.length > 0) { d = docs[0]; }
}
return d;
}

// @Override
// public String getShortDescription() {
// final doc d = getDocAnnotation();
// final String doc = d == null ? null : d.value();
// String s = super.getShortDescription();
// if (getEnclosingDescription() != null && (getEnclosingDescription().redefinesAction(getName()) || isBuiltIn())
// && doc != null && !doc.isEmpty()) {
// s += ": " + doc + "<br/>";
// }
// return s;
//
// }
/**
* Gets the args.
*
* @return the args
*/
public Map<String, arg> getArgs() {
if (method == null || !method.isAnnotationPresent(action.class)) return Collections.EMPTY_MAP;
action annot = method.getAnnotation(action.class);
arg[] list = annot.args();
if (list.length == 0) return Collections.EMPTY_MAP;
Map<String, arg> result = new LinkedHashMap<>();
for (arg a : list) { result.put(a.name(), a); }
return result;
}

/**
* Gets the helper.
*
* @return the helper
*/
public IGamaHelper getHelper() {
return helper;
}
public IGamaHelper getHelper() { return helper; }

@Override
public PrimitiveDescription validate() {
Expand All @@ -156,8 +196,10 @@ public PrimitiveDescription validate() {
/**
* Sets the helper.
*
* @param helper the helper
* @param method the method
* @param helper
* the helper
* @param method
* the method
*/
public void setHelper(final IGamaHelper helper, final AccessibleObject method) {
this.helper = helper;
Expand All @@ -177,9 +219,7 @@ public PrimitiveDescription copy(final IDescription into) {
* name
*/
@Override
public void setDefiningPlugin(final String plugin) {
this.plugin = plugin;
}
public void setDefiningPlugin(final String plugin) { this.plugin = plugin; }

// @Override
// public void collectMetaInformation(final GamlProperties meta) {
Expand Down

0 comments on commit 847443e

Please sign in to comment.