diff --git a/msi.gama.core/src/msi/gaml/compilation/AbstractGamlAdditions.java b/msi.gama.core/src/msi/gaml/compilation/AbstractGamlAdditions.java index 643a747fef..afa534537a 100644 --- a/msi.gama.core/src/msi/gaml/compilation/AbstractGamlAdditions.java +++ b/msi.gama.core/src/msi/gaml/compilation/AbstractGamlAdditions.java @@ -1,9 +1,9 @@ /******************************************************************************************************* * - * msi.gaml.compilation.AbstractGamlAdditions.java, in plugin msi.gama.core, is part of the source code of the GAMA - * modeling and simulation platform (v. 1.8.1) + * AbstractGamlAdditions.java, in msi.gama.core, is part of the source code of the GAMA modeling and simulation platform + * (v.2.0.0). * - * (c) 2007-2020 UMI 209 UMMISCO IRD/SU & Partners + * (c) 2007-2021 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU) * * Visit https://github.com/gama-platform/gama for license information and contacts. * @@ -41,6 +41,8 @@ import com.google.common.base.Function; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; +import com.google.common.collect.MultimapBuilder; +import com.google.common.collect.SetMultimap; import msi.gama.common.interfaces.IDisplayCreator; import msi.gama.common.interfaces.IDisplayCreator.DisplayDescription; @@ -53,7 +55,6 @@ import msi.gama.precompiler.ISymbolKind; import msi.gama.precompiler.ITypeProvider; import msi.gama.util.GamaMapFactory; -import msi.gama.util.IMap; import msi.gama.util.file.IGamaFile; import msi.gaml.compilation.annotations.serializer; import msi.gaml.compilation.annotations.validator; @@ -92,73 +93,211 @@ @SuppressWarnings ({ "unchecked", "rawtypes" }) public abstract class AbstractGamlAdditions implements IGamlAdditions { + /** The Constant CONSTANTS. */ public static final Set CONSTANTS = new HashSet(); + + /** The Constant ADDITIONS. */ final static Multimap ADDITIONS = HashMultimap.create(); + + /** The into descriptions. */ private static Function> INTO_DESCRIPTIONS = input -> ADDITIONS.get(input); + + /** The Constant FIELDS. */ private final static Multimap FIELDS = HashMultimap.create(); + + /** The Constant VARTYPE2KEYWORDS. */ public final static Multimap VARTYPE2KEYWORDS = HashMultimap.create(); + + /** The Constant TEMPORARY_BUILT_IN_VARS_DOCUMENTATION. */ public final static Map TEMPORARY_BUILT_IN_VARS_DOCUMENTATION = new HashMap<>(); + + /** The Constant LISTENERS_BY_CLASS. */ public final static HashMultimap LISTENERS_BY_CLASS = HashMultimap.create(); + + /** The Constant LISTENERS_BY_NAME. */ public final static HashMultimap LISTENERS_BY_NAME = HashMultimap.create(); + /** + * S. + * + * @param strings + * the strings + * @return the string[] + */ protected static String[] S(final String... strings) { return strings; } + /** + * I. + * + * @param integers + * the integers + * @return the int[] + */ protected static int[] I(final int... integers) { return integers; } + /** + * P. + * + * @param protos + * the protos + * @return the facet proto[] + */ protected static FacetProto[] P(final FacetProto... protos) { return protos; } + /** + * C. + * + * @param classes + * the classes + * @return the class[] + */ protected static Class[] C(final Class... classes) { return classes; } + /** + * T. + * + * @param c + * the c + * @return the i type + */ protected static IType T(final Class c) { return Types.get(c); } + /** + * Ti. + * + * @param c + * the c + * @return the string + */ protected static String Ti(final Class c) { return String.valueOf(Types.get(c).id()); } + /** + * Ts. + * + * @param c + * the c + * @return the string + */ protected static String Ts(final Class c) { return Types.get(c).toString(); } + /** + * T. + * + * @param c + * the c + * @return the i type + */ protected static IType T(final String c) { return Types.get(c); } + /** + * T. + * + * @param c + * the c + * @return the i type + */ protected static IType T(final int c) { return Types.get(c); } + /** + * Display. + * + * @param string + * the string + * @param d + * the d + */ public void _display(final String string, final IDisplayCreator d) { CONSTANTS.add(string); final DisplayDescription dd = new DisplayDescription(d, string, CURRENT_PLUGIN_NAME); IGui.DISPLAYS.put(string, dd); } + /** + * Experiment. + * + * @param string + * the string + * @param d + * the d + */ public void _experiment(final String string, final IExperimentAgentCreator d) { CONSTANTS.add(string); final ExperimentAgentDescription ed = new ExperimentAgentDescription(d, string, CURRENT_PLUGIN_NAME); GamaMetaModel.INSTANCE.addExperimentAgentCreator(string, ed); } + /** + * Species. + * + * @param name + * the name + * @param clazz + * the clazz + * @param helper + * the helper + * @param skills + * the skills + */ public void _species(final String name, final Class clazz, final IAgentConstructor helper, final String... skills) { GamaMetaModel.INSTANCE.addSpecies(name, clazz, helper, skills); // DescriptionFactory.addSpeciesNameAsType(name); } + /** + * Type. + * + * @param keyword + * the keyword + * @param typeInstance + * the type instance + * @param id + * the id + * @param varKind + * the var kind + * @param wraps + * the wraps + */ protected void _type(final String keyword, final IType typeInstance, final int id, final int varKind, final Class... wraps) { initType(keyword, typeInstance, id, varKind, wraps); } + /** + * File. + * + * @param string + * the string + * @param clazz + * the clazz + * @param helper + * the helper + * @param innerType + * the inner type + * @param keyType + * the key type + * @param contentType + * the content type + * @param s + * the s + */ protected void _file(final String string, final Class clazz, final GamaGetter.Unary> helper, final int innerType, final int keyType, final int contentType, final String[] s) { // helper.setSkillClass(clazz); @@ -167,16 +306,62 @@ protected void _file(final String string, final Class clazz, final GamaGetter.Un VARTYPE2KEYWORDS.put(ISymbolKind.Variable.CONTAINER, string + "_file"); } + /** + * Skill. + * + * @param name + * the name + * @param clazz + * the clazz + * @param species + * the species + */ protected void _skill(final String name, final Class clazz, final String... species) { GamaSkillRegistry.INSTANCE.register(name, clazz, CURRENT_PLUGIN_NAME, ADDITIONS.get(clazz), species); } + /** + * Factories. + * + * @param factories + * the factories + */ protected void _factories(final SymbolFactory... factories) { - for (final SymbolFactory f : factories) { - DescriptionFactory.addFactory(f); - } + for (final SymbolFactory f : factories) { DescriptionFactory.addFactory(f); } } + /** + * Symbol. + * + * @param names + * the names + * @param c + * the c + * @param sKind + * the s kind + * @param remote + * the remote + * @param args + * the args + * @param scope + * the scope + * @param sequence + * the sequence + * @param unique + * the unique + * @param name_unique + * the name unique + * @param contextKeywords + * the context keywords + * @param contextKinds + * the context kinds + * @param fmd + * the fmd + * @param omissible + * the omissible + * @param sc + * the sc + */ protected void _symbol(final String[] names, final Class c, final int sKind, final boolean remote, final boolean args, final boolean scope, final boolean sequence, final boolean unique, final boolean name_unique, final String[] contextKeywords, final int[] contextKinds, final FacetProto[] fmd, @@ -198,11 +383,7 @@ protected void _symbol(final String[] names, final Class c, final int sKind, fin } else { keywords = Arrays.asList(names); } - if (fmd != null) { - for (final FacetProto f : fmd) { - f.buildDoc(c); - } - } + if (fmd != null) { for (final FacetProto f : fmd) { f.buildDoc(c); } } final SymbolProto md = new SymbolProto(c, sequence, args, sKind, !scope, fmd, omissible, contextKeywords, contextKinds, remote, unique, name_unique, sc, validator2, serializer2, @@ -210,6 +391,32 @@ protected void _symbol(final String[] names, final Class c, final int sKind, fin DescriptionFactory.addProto(md, keywords); } + /** + * Iterator. + * + * @param keywords + * the keywords + * @param method + * the method + * @param classes + * the classes + * @param expectedContentTypes + * the expected content types + * @param ret + * the ret + * @param c + * the c + * @param t + * the t + * @param content + * the content + * @param index + * the index + * @param contentContentType + * the content content type + * @param helper + * the helper + */ public void _iterator(final String[] keywords, final Method method, final Class[] classes, final int[] expectedContentTypes, final Class ret, final boolean c, final int t, final int content, final int index, final int contentContentType, final GamaGetter.Binary helper) { @@ -217,6 +424,32 @@ public void _iterator(final String[] keywords, final Method method, final Class[ _binary(keywords, method, classes, expectedContentTypes, ret, c, t, content, index, contentContentType, helper); } + /** + * Binary. + * + * @param keywords + * the keywords + * @param method + * the method + * @param classes + * the classes + * @param expectedContentTypes + * the expected content types + * @param returnClassOrType + * the return class or type + * @param c + * the c + * @param t + * the t + * @param content + * the content + * @param index + * the index + * @param contentContentType + * the content content type + * @param helper + * the helper + */ public void _binary(final String[] keywords, final AccessibleObject method, final Class[] classes, final int[] expectedContentTypes, final Object returnClassOrType, final boolean c, final int t, final int content, final int index, final int contentContentType, final GamaGetter.Binary helper) { @@ -235,7 +468,7 @@ public void _binary(final String[] keywords, final AccessibleObject method, fina rt = (IType) returnClassOrType; } // binary - if ((kw.equals(OF) || kw.equals(_DOT)) && signature.get(0).isAgentType()) { + if ((OF.equals(kw) || _DOT.equals(kw)) && signature.get(0).isAgentType()) { proto = new OperatorProto(kw, method, helper, c, true, rt, signature, IExpression.class.equals(classes[1]), t, content, index, contentContentType, expectedContentTypes, plugin); @@ -251,6 +484,32 @@ public void _binary(final String[] keywords, final AccessibleObject method, fina } + /** + * Operator. + * + * @param keywords + * the keywords + * @param method + * the method + * @param classes + * the classes + * @param expectedContentTypes + * the expected content types + * @param returnClassOrType + * the return class or type + * @param c + * the c + * @param t + * the t + * @param content + * the content + * @param index + * the index + * @param contentContentType + * the content content type + * @param helper + * the helper + */ public void _operator(final String[] keywords, final AccessibleObject method, final Class[] classes, final int[] expectedContentTypes, final Object returnClassOrType, final boolean c, final int t, final int content, final int index, final int contentContentType, final GamaGetter.NAry helper) { @@ -273,7 +532,7 @@ public void _operator(final String[] keywords, final AccessibleObject method, fi IExpression.class.equals(classes[0]), t, content, index, contentContentType, expectedContentTypes, plugin); } else if (classes.length == 2) { // binary - if ((kw.equals(OF) || kw.equals(_DOT)) && signature.get(0).isAgentType()) { + if ((OF.equals(kw) || _DOT.equals(kw)) && signature.get(0).isAgentType()) { proto = new OperatorProto(kw, method, helper, c, true, rt, signature, IExpression.class.equals(classes[1]), t, content, index, contentContentType, expectedContentTypes, plugin); @@ -293,12 +552,48 @@ public void _operator(final String[] keywords, final AccessibleObject method, fi } + /** + * Listener. + * + * @param varName + * the var name + * @param clazz + * the clazz + * @param helper + * the helper + */ public void _listener(final String varName, final Class clazz, final IGamaHelper helper) { GamaHelper gh = new GamaHelper(varName, clazz, helper); LISTENERS_BY_CLASS.put(clazz, gh); LISTENERS_BY_NAME.put(varName, clazz); } + /** + * Unary. + * + * @param keywords + * the keywords + * @param method + * the method + * @param classes + * the classes + * @param expectedContentTypes + * the expected content types + * @param returnClassOrType + * the return class or type + * @param c + * the c + * @param t + * the t + * @param content + * the content + * @param index + * the index + * @param contentContentType + * the content content type + * @param helper + * the helper + */ public void _unary(final String[] keywords, final AccessibleObject method, final Class[] classes, final int[] expectedContentTypes, final Object returnClassOrType, final boolean c, final int t, final int content, final int index, final int contentContentType, final GamaGetter.Unary helper) { @@ -325,6 +620,26 @@ public void _unary(final String[] keywords, final AccessibleObject method, final } + /** + * Operator. + * + * @param keywords + * the keywords + * @param method + * the method + * @param classes + * the classes + * @param expectedContentTypes + * the expected content types + * @param ret + * the ret + * @param c + * the c + * @param typeAlias + * the type alias + * @param helper + * the helper + */ // For files public void _operator(final String[] keywords, final AccessibleObject method, final Class[] classes, final int[] expectedContentTypes, final Class ret, final boolean c, final String typeAlias, @@ -341,6 +656,26 @@ public void _operator(final String[] keywords, final AccessibleObject method, fi ITypeProvider.NONE, ITypeProvider.NONE, helper); } + /** + * Binary. + * + * @param keywords + * the keywords + * @param method + * the method + * @param classes + * the classes + * @param expectedContentTypes + * the expected content types + * @param ret + * the ret + * @param c + * the c + * @param typeAlias + * the type alias + * @param helper + * the helper + */ public void _binary(final String[] keywords, final AccessibleObject method, final Class[] classes, final int[] expectedContentTypes, final Class ret, final boolean c, final String typeAlias, final GamaGetter.Binary helper) { @@ -356,10 +691,32 @@ public void _binary(final String[] keywords, final AccessibleObject method, fina ITypeProvider.NONE, ITypeProvider.NONE, helper); } + /** + * Adds the. + * + * @param clazz + * the clazz + * @param desc + * the desc + */ private void add(final Class clazz, final IDescription desc) { ADDITIONS.put(clazz, desc); } + /** + * Var. + * + * @param clazz + * the clazz + * @param desc + * the desc + * @param get + * the get + * @param init + * the init + * @param set + * the set + */ protected void _var(final Class clazz, final IDescription desc, final IGamaHelper get, final IGamaHelper init, final IGamaHelper set) { add(clazz, desc); @@ -368,6 +725,15 @@ protected void _var(final Class clazz, final IDescription desc, final IGamaHelpe ((VariableDescription) desc).setDefiningPlugin(GamaBundleLoader.CURRENT_PLUGIN_NAME); } + /** + * Gets the var doc. + * + * @param name + * the name + * @param clazz + * the clazz + * @return the var doc + */ private String getVarDoc(final String name, final Class clazz) { final vars vars = clazz.getAnnotationsByType(vars.class)[0]; for (final msi.gama.precompiler.GamlAnnotations.variable v : vars.value()) { @@ -381,25 +747,93 @@ private String getVarDoc(final String name, final Class clazz) { return ""; } + /** + * Facet. + * + * @param name + * the name + * @param types + * the types + * @param ct + * the ct + * @param kt + * the kt + * @param values + * the values + * @param optional + * the optional + * @param internal + * the internal + * @param isRemote + * the is remote + * @return the facet proto + */ protected FacetProto _facet(final String name, final int[] types, final int ct, final int kt, final String[] values, final boolean optional, final boolean internal, final boolean isRemote) { return new FacetProto(name, types, ct, kt, values, optional, internal, isRemote); } + /** + * Proto. + * + * @param name + * the name + * @param helper + * the helper + * @param returnType + * the return type + * @param signature + * the signature + * @param typeProvider + * the type provider + * @param contentTypeProvider + * the content type provider + * @param keyTypeProvider + * the key type provider + * @return the operator proto + */ protected OperatorProto _proto(final String name, final GamaGetter.Unary helper, final int returnType, final Class signature, final int typeProvider, final int contentTypeProvider, final int keyTypeProvider) { return new OperatorProto(name, null, helper, false, true, returnType, signature, false, typeProvider, contentTypeProvider, keyTypeProvider, AI); } + /** + * Field. + * + * @param clazz + * the clazz + * @param getter + * the getter + */ protected void _field(final Class clazz, final OperatorProto getter) { FIELDS.put(clazz, getter); } + /** + * Desc. + * + * @param keyword + * the keyword + * @param children + * the children + * @param facets + * the facets + * @return the i description + */ protected IDescription desc(final String keyword, final Children children, final String... facets) { return create(keyword, null, children.getChildren(), facets); } + /** + * Desc. + * + * @param keyword + * the keyword + * @param facets + * the facets + * @return the i description + */ protected IDescription desc(final String keyword, final String... facets) { return create(keyword, facets); } @@ -417,6 +851,16 @@ protected IDescription desc(final int keyword, final String... facets) { return desc(t.toString(), facets); } + /** + * Action. + * + * @param e + * the e + * @param desc + * the desc + * @param method + * the method + */ protected void _action(final IGamaHelper e, final IDescription desc, final Method method) { final Class clazz = method.getDeclaringClass(); ((PrimitiveDescription) desc).setHelper(new GamaHelper(desc.getName(), clazz, e), method); @@ -424,42 +868,83 @@ protected void _action(final IGamaHelper e, final IDescription desc, final Metho add(clazz, desc); } + /** + * Inits the type. + * + * @param keyword + * the keyword + * @param typeInstance + * the type instance + * @param id + * the id + * @param varKind + * the var kind + * @param wraps + * the wraps + */ public static void initType(final String keyword, final IType typeInstance, final int id, final int varKind, final Class... wraps) { final IType type = Types.builtInTypes.initType(keyword, typeInstance, id, varKind, wraps[0]); - for (final Class cc : wraps) { - Types.CLASSES_TYPES_CORRESPONDANCE.put(cc, type.getName()); - } + for (final Class cc : wraps) { Types.CLASSES_TYPES_CORRESPONDANCE.put(cc, type.getName()); } type.setDefiningPlugin(GamaBundleLoader.CURRENT_PLUGIN_NAME); Types.cache(id, typeInstance); VARTYPE2KEYWORDS.put(varKind, keyword); } + /** + * Gets the additions. + * + * @param clazz + * the clazz + * @return the additions + */ public static Collection getAdditions(final Class clazz) { return ADDITIONS.get(clazz); } + /** + * Gets the all fields. + * + * @param clazz + * the clazz + * @return the all fields + */ public static Map getAllFields(final Class clazz) { final List classes = collectImplementationClasses(clazz, Collections.EMPTY_SET, FIELDS.keySet()); final Map fieldsMap = create(); for (final Class c : classes) { - for (final OperatorProto desc : FIELDS.get(c)) { - fieldsMap.put(desc.getName(), desc); - } + for (final OperatorProto desc : FIELDS.get(c)) { fieldsMap.put(desc.getName(), desc); } } return fieldsMap; } + /** + * Gets the all children of. + * + * @param base + * the base + * @param skills + * the skills + * @return the all children of + */ public static Iterable getAllChildrenOf(final Class base, final Iterable> skills) { final List classes = collectImplementationClasses(base, skills, ADDITIONS.keySet()); return concat(transform(classes, INTO_DESCRIPTIONS)); } - public static Collection getAllFields() { - return FIELDS.values(); - } + /** + * Gets the all fields. + * + * @return the all fields + */ + public static Collection getAllFields() { return FIELDS.values(); } + /** + * Gets the all vars. + * + * @return the all vars + */ public static Collection getAllVars() { final HashSet result = new HashSet<>(); @@ -487,6 +972,13 @@ public static Collection getAllVars() { return result; } + /** + * Gets the statements for skill. + * + * @param s + * the s + * @return the statements for skill + */ public static Collection getStatementsForSkill(final String s) { final Set result = new LinkedHashSet(); for (final String p : getStatementProtoNames()) { @@ -496,17 +988,20 @@ public static Collection getStatementsForSkill(final String s) { return result; } + /** + * Gets the all actions. + * + * @return the all actions + */ public static Collection getAllActions() { - final IMap result = createUnordered(); + SetMultimap result = MultimapBuilder.hashKeys().linkedHashSetValues().build(); final DescriptionVisitor visitor = desc -> { - result.putIfAbsent(desc.getName(), desc); + result.put(desc.getName(), desc); return true; }; - for (final TypeDescription s : getBuiltInSpecies()) { - s.visitOwnActions(visitor); - } + for (final TypeDescription s : getBuiltInSpecies()) { s.visitOwnActions(visitor); } GamaSkillRegistry.INSTANCE.visitSkills(desc -> { ((SkillDescription) desc).visitOwnActions(visitor); return true; @@ -514,12 +1009,14 @@ public static Collection getAllActions() { return result.values(); } + /** + * Constants. + * + * @param strings + * the strings + */ public static void _constants(final String[]... strings) { - for (final String[] s : strings) { - for (final String s2 : s) { - CONSTANTS.add(s2); - } - } + for (final String[] s : strings) { Collections.addAll(CONSTANTS, s); } } /** @@ -529,9 +1026,7 @@ public static void _constants(final String[]... strings) { public static boolean isUnaryOperator(final String name) { if (!OPERATORS.containsKey(name)) return false; final Map map = OPERATORS.get(name); - for (final Signature s : map.keySet()) { - if (s.isUnary()) return true; - } + for (final Signature s : map.keySet()) { if (s.isUnary()) return true; } return false; }