/** Create an expression but does not change the container. * * @param expression the textual representation of the expression. * @return the expression. */ @Pure protected XExpression fromString(String expression) { if (!Strings.isEmpty(expression)) { ResourceSet resourceSet = this.context.eResource().getResourceSet(); URI uri = computeUnusedUri(resourceSet); Resource resource = getResourceFactory().createResource(uri); resourceSet.getResources().add(resource); try (StringInputStream is = new StringInputStream(generateExpressionCode(expression))) { resource.load(is, null); SarlScript script = resource.getContents().isEmpty() ? null : (SarlScript) resource.getContents().get(0); SarlEvent topElement = (SarlEvent) script.getXtendTypes().get(0); SarlField member = (SarlField) topElement.getMembers().get(0); return member.getInitialValue(); } catch (Throwable exception) { throw new RuntimeException(exception); } finally { resourceSet.getResources().remove(resource); } } throw new IllegalArgumentException("not a valid expression"); }
/** Create a field declaration. * * @param field the field to generate. * @param it the output * @param context the generation context. */ protected void generatePythonField(SarlField field, PyAppendable it, IExtraLanguageGeneratorContext context) { generateBlockComment(getTypeBuilder().getDocumentation(field), it); if (!field.isStatic()) { it.append("self."); //$NON-NLS-1$ } final String fieldName = it.declareUniqueNameVariable(field, field.getName()); it.append(fieldName); it.append(" = "); //$NON-NLS-1$ if (field.getInitialValue() != null) { generate(field.getInitialValue(), null, it, context); } else { it.append(PyExpressionGenerator.toDefaultValue(field.getType())); } it.newLine(); }