@Override public int compare(SarlBehaviorUnit o1, SarlBehaviorUnit o2) { if (o1 == o2) { return 0; } if (o1 == null) { return Integer.MIN_VALUE; } if (o2 == null) { return Integer.MAX_VALUE; } final int cmp = o1.getName().getIdentifier().compareTo(o2.getName().getIdentifier()); if (cmp != 0) { return cmp; } return compare(o1.getGuard(), o2.getGuard()); }
/** Replies the resource. */ @Pure public Resource eResource() { return getSarlBehaviorUnit().eResource(); }
/** Initialize the Ecore element. * @param container the container of the SarlBehaviorUnit. * @param name the type of the SarlBehaviorUnit. */ public void eInit(XtendTypeDeclaration container, String name, IJvmTypeProvider context) { setTypeResolutionContext(context); if (this.sarlBehaviorUnit == null) { this.container = container; this.sarlBehaviorUnit = SarlFactory.eINSTANCE.createSarlBehaviorUnit(); this.sarlBehaviorUnit.setAnnotationInfo(XtendFactory.eINSTANCE.createXtendMember()); this.sarlBehaviorUnit.setName(newTypeRef(container, name)); container.getMembers().add(this.sarlBehaviorUnit); } }
final JvmTypeReference event = handler.getName(); final String handleName = it.declareUniqueNameVariable(handler, "__on_" + event.getSimpleName() + "__"); //$NON-NLS-1$ //$NON-NLS-2$ it.append("def ").append(handleName).append("(self, occurrence):"); //$NON-NLS-1$ //$NON-NLS-2$ it.increaseIndentation().newLine(); generateDocString(getTypeBuilder().getDocumentation(handler), it); if (handler.getExpression() != null) { generate(handler.getExpression(), null, it, context); } else { it.append("pass"); //$NON-NLS-1$ final String key = this.qualifiedNameProvider.getFullyQualifiedName(handler.getDeclaringType()).toString(); final Map<String, Map<String, List<Pair<XExpression, String>>>> map = context.getMapData(EVENT_GUARDS_MEMENTO); Map<String, List<Pair<XExpression, String>>> submap = map.get(key); submap.put(eventId, guards); guards.add(new Pair<>(handler.getGuard(), handleName));
if (source.getName() != null && !Strings.isNullOrEmpty(source.getName().getSimpleName()) && context != null) { final XExpression guard = source.getGuard(); final String bodyMethodName = Utils.createNameForHiddenEventHandlerMethod(source.getName().getSimpleName(), context.getBehaviorUnitIndex()); jvmParam.setParameterType(this.typeBuilder.cloneWithProxies(source.getName())); this.associator.associate(source, jvmParam); bodyOperation.getParameters().add(jvmParam); setBody(bodyOperation, source.getExpression()); translateAnnotationsTo(source.getAnnotations(), bodyOperation); if (context.getGeneratorConfig2().isGeneratePureAnnotation() && !this.services.getExpressionHelper().hasSideEffects(source.getExpression())) { addAnnotationSafe(bodyOperation, Pure.class); final String guardMethodName = Utils.createNameForHiddenGuardEvaluatorMethod(source.getName().getSimpleName(), context.getBehaviorUnitIndex()); jvmParam.setParameterType(this.typeBuilder.cloneWithProxies(source.getName())); this.associator.associate(source, jvmParam); guardOperation.getParameters().add(jvmParam); jvmParam.setParameterType(this.typeBuilder.cloneWithProxies(source.getName())); this.associator.associate(source, jvmParam); guardOperation.getParameters().add(jvmParam);
/** Format a behavior unit. * * @param behaviorUnit the behavior unit. * @param document the document. */ protected void _format(SarlBehaviorUnit behaviorUnit, IFormattableDocument document) { formatAnnotations(behaviorUnit, document, XbaseFormatterPreferenceKeys.newLineAfterMethodAnnotations); final ISemanticRegionsFinder regionFor = this.textRegionExtensions.regionFor(behaviorUnit); document.append(regionFor.keyword(this.keywords.getOnKeyword()), ONE_SPACE); if (behaviorUnit.getGuard() != null) { ISemanticRegion keyword = this.textRegionExtensions.immediatelyPreceding( behaviorUnit.getGuard()).keyword(this.keywords.getLeftSquareBracketKeyword()); document.prepend(keyword, ONE_SPACE); document.append(keyword, NO_SPACE); keyword = this.textRegionExtensions.immediatelyFollowing( behaviorUnit.getGuard()).keyword(this.keywords.getRightSquareBracketKeyword()); document.prepend(keyword, NO_SPACE); } document.format(behaviorUnit.getName()); document.format(behaviorUnit.getGuard()); final XExpression expression = behaviorUnit.getExpression(); if (expression != null) { final ISemanticRegionFinder finder = this.textRegionExtensions.regionFor(expression); final ISemanticRegion brace = finder.keyword(this.keywords.getLeftCurlyBracketKeyword()); document.prepend(brace, XbaseFormatterPreferenceKeys.bracesInNewLine); document.format(expression); } }
/** Replies the guard evaluation code for the given event. * * @param source the source of the guard evaluation. * @return the guard evaluators. */ public Collection<Procedure1<? super ITreeAppendable>> getGuardEvalationCodeFor(SarlBehaviorUnit source) { assert source != null; final String id = source.getName().getIdentifier(); final Collection<Procedure1<? super ITreeAppendable>> evaluators; final Pair<SarlBehaviorUnit, Collection<Procedure1<? super ITreeAppendable>>> pair = this.guardEvaluators.get(id); if (pair == null) { evaluators = new ArrayList<>(); this.guardEvaluators.put(id, new Pair<>(source, evaluators)); } else { evaluators = pair.getValue(); assert evaluators != null; } return evaluators; }
@Override public JvmOperation getInferredGuardOperation(SarlBehaviorUnit behaviorUnit) { final XExpression guard = behaviorUnit.getGuard(); if (guard != null) { final EObject primaryJvmElement = getPrimaryJvmElement(guard); if (primaryJvmElement instanceof JvmOperation) { return (JvmOperation) primaryJvmElement; } } return null; }
/** Check for usage of the event functions in the behavior units. * * @param unit the unit to analyze. */ @Check(CheckType.EXPENSIVE) public void checkUnmodifiableEventAccess(SarlBehaviorUnit unit) { final boolean enable1 = !isIgnored(IssueCodes.DISCOURAGED_OCCURRENCE_READONLY_USE); final XExpression root = unit.getExpression(); final String occurrenceKw = this.grammarAccess.getOccurrenceKeyword(); for (final XFeatureCall child : EcoreUtil2.getAllContentsOfType(root, XFeatureCall.class)) { if (occurrenceKw.equals(child.getFeature().getIdentifier())) { checkUnmodifiableFeatureAccess(enable1, child, occurrenceKw); } } }
/** Create the block of code. * @return the block builder. */ public IBlockExpressionBuilder getExpression() { IBlockExpressionBuilder block = this.blockExpressionProvider.get(); block.eInit(getTypeResolutionContext()); XBlockExpression expr = block.getXBlockExpression(); this.sarlBehaviorUnit.setExpression(expr); return block; }
/** Change the documentation of the element. * * <p>The documentation will be displayed just before the element. * * @param doc the documentation. */ public void setDocumentation(String doc) { if (Strings.isEmpty(doc)) { getSarlBehaviorUnit().eAdapters().removeIf(new Predicate<Adapter>() { public boolean test(Adapter adapter) { return adapter.isAdapterForType(DocumentationAdapter.class); } }); } else { DocumentationAdapter adapter = (DocumentationAdapter) EcoreUtil.getExistingAdapter( getSarlBehaviorUnit(), DocumentationAdapter.class); if (adapter == null) { adapter = new DocumentationAdapter(); getSarlBehaviorUnit().eAdapters().add(adapter); } adapter.setDocumentation(doc); } }
/** Check if the parameter of the bahavior unit is an event. * * @param behaviorUnit the behavior unit to test. */ @Check(CheckType.FAST) public void checkBehaviorUnitEventType(SarlBehaviorUnit behaviorUnit) { final JvmTypeReference event = behaviorUnit.getName(); final LightweightTypeReference ref = toLightweightTypeReference(event); if (ref == null || !this.inheritanceHelper.isSarlEvent(ref)) { error(MessageFormat.format( Messages.SARLValidator_75, event.getQualifiedName(), Messages.SARLValidator_62, this.grammarAccess.getOnKeyword()), event, null, ValidationMessageAcceptor.INSIGNIFICANT_INDEX, TYPE_BOUNDS_MISMATCH); } }
final SarlBehaviorUnit source = evaluators.getKey(); final String behName = Utils.createNameForHiddenGuardGeneralEvaluatorMethod(source.getName().getSimpleName()); jvmParam.setParameterType(this.typeBuilder.cloneWithProxies(source.getName())); this.associator.associate(source, jvmParam); operation.getParameters().add(jvmParam);
/** Replies the resource. */ @Pure public Resource eResource() { return getSarlBehaviorUnit().eResource(); }