use of org.lara.language.specification.dsl.Attribute in project lara-framework by specs-feup.
the class LanguageSpecificationSideBar method getAttributes.
private List<Attribute> getAttributes(JoinPointClass joinPoint) {
switch(sortingMethod) {
case ALPHABETICALLY:
return getAlphabetical(joinPoint, jp -> jp.getAttributes());
case HIERARCHICALLY:
List<Attribute> attributes = new ArrayList<>();
getHierarchical(joinPoint, attributes, jp -> jp.getAttributesSelf(), jp -> new Attribute(new GenericType(SEPARATOR_TYPE, false), jp.getName()));
return attributes;
default:
throw new NotImplementedException(sortingMethod);
}
}
use of org.lara.language.specification.dsl.Attribute in project lara-framework by specs-feup.
the class LangSpecsXmlParser method parse.
public static LanguageSpecificationV2 parse(InputStream joinPointModel, InputStream attributeModel, InputStream actionModel, boolean validate) {
// System.out.println("JP SCHEMA: " + SchemaResource.JOIN_POINT_SCHEMA.read());
// System.out.println("JP SCHEMA: " + SchemaResource.JOIN_POINT_SCHEMA.getResource());
var jpSchema = validate ? SchemaResource.JOIN_POINT_SCHEMA.toStream() : null;
var attrSchema = validate ? SchemaResource.ATTRIBUTE_SCHEMA.toStream() : null;
var actionSchema = validate ? SchemaResource.ACTION_SCHEMA.toStream() : null;
var joinPointModelNode = XmlDocument.newInstance(joinPointModel, jpSchema);
var attributeModelNode = XmlDocument.newInstance(attributeModel, attrSchema);
var actionModelNode = XmlDocument.newInstance(actionModel, actionSchema);
// Setup global JoinPointClass
LanguageSpecificationV2 langSpecV2 = new LanguageSpecificationV2();
JoinPointClass global = JoinPointClass.globalJoinPoint(langSpecV2);
langSpecV2.setGlobal(global);
// Initialize types (typedef, enums), to have access to available names
for (var type : attributeModelNode.getElementsByName("object")) {
var typeDef = new TypeDef(type.getAttribute("name"));
langSpecV2.add(typeDef);
setOptional(type.getAttribute("tooltip"), typeDef::setToolTip);
}
for (var type : attributeModelNode.getElementsByName("enum")) {
var enumDef = new EnumDef(type.getAttribute("name"));
langSpecV2.add(enumDef);
setOptional(type.getAttribute("tooltip"), enumDef::setToolTip);
List<EnumValue> valuesList = toEnumValues(type.getElementsByName("value"), langSpecV2);
enumDef.setValues(valuesList);
}
List<JoinPointClass> jps = new ArrayList<>();
for (var jpNode : joinPointModelNode.getElementsByName("joinpoint")) {
var jp = new JoinPointClass(jpNode.getAttribute("class"), langSpecV2);
setOptional(jpNode.getAttribute("tooltip"), jp::setToolTip);
jps.add(jp);
}
Collections.sort(jps);
jps.stream().forEach(langSpecV2::add);
var joinpoints = joinPointModelNode.getElementsByName("joinpoints").get(0);
langSpecV2.setRoot(joinpoints.getAttribute("root_class"));
setOptional(joinpoints.getAttribute("root_alias"), langSpecV2::setRootAlias);
// Map of actions according to class
MultiMap<String, XmlElement> joinPointActions = new MultiMap<>();
List<XmlElement> globalActions = new ArrayList<>();
for (var actionNode : actionModelNode.getElementsByName("action")) {
var classNames = actionNode.getAttribute("class");
// Global actions do not have a class value, or its value is '*'
if (classNames.isEmpty() || classNames.equals("*")) {
globalActions.add(actionNode);
continue;
}
// System.out.println("CLASS NAMES: " + classNames);
for (String className : classNames.split(",")) {
// System.out.println("NAME: " + className);
joinPointActions.add(className.strip(), actionNode);
}
}
populateGlobal(joinPointModelNode, attributeModelNode, actionModelNode, langSpecV2, global, globalActions);
// Populate TypeDef
for (var typeNode : attributeModelNode.getElementsByName("object")) {
TypeDef typeDef = langSpecV2.getTypeDefs().get(typeNode.getAttribute("name"));
List<Attribute> attributesList = convertAttributes(typeNode.getElementsByName("attribute"), langSpecV2);
typeDef.setFields(attributesList);
}
for (var jpNode : joinPointModelNode.getElementsByName("joinpoint")) {
String jpClass = jpNode.getAttribute("class");
JoinPointClass jp = langSpecV2.getJoinPoint(jpClass);
String extendsType = jpNode.getAttribute("extends");
if (!extendsType.isEmpty()) {
jp.setExtend(langSpecV2.getJoinPoint(extendsType));
} else {
jp.setExtend(global);
}
// Obtain attribute nodes from artifacts
List<XmlElement> artifactNodes = attributeModelNode.getElementsByName("artifact").stream().filter(attribute -> attribute.getAttribute("class").equals(jpClass)).collect(Collectors.toList());
var attributeNodes = artifactNodes.stream().flatMap(art -> art.getElementsByName("attribute").stream()).collect(Collectors.toList());
// Add attributes
jp.setAttributes(convertAttributes(attributeNodes, langSpecV2));
// Add selects
jp.setSelects(convertSelects(langSpecV2, jpNode.getElementsByName("select")));
// Add actions
jp.setActions(convertActions(langSpecV2, joinPointActions.get(jpClass)));
// Set default attributes
for (var artifact : attributeModelNode.getElementsByName("artifact")) {
var defaultValue = artifact.getAttribute("default");
if (defaultValue.isEmpty()) {
continue;
}
// Get corresponding join point and set default
// System.out.println("ARTIFACT CLASS: " + artifact.getAttribute("class"));
// System.out.println("JP: " + langSpecV2.getJoinPoint(artifact.getAttribute("class")));
var artifactJp = langSpecV2.getJoinPoint(artifact.getAttribute("class"));
if (artifactJp == null) {
SpecsLogs.info("Artifact without join point: " + artifact.getAttribute("class"));
continue;
}
artifactJp.setDefaultAttribute(defaultValue);
// System.out.println("SETTING DEFAULT '" + defaultValue + "' for JP " +
// artifact.getAttribute("class"));
}
}
// Add default global attributes (e.g., joinPointType, instanceOf)
addDefaultGlobalAttributes(langSpecV2);
return langSpecV2;
}
use of org.lara.language.specification.dsl.Attribute in project lara-framework by specs-feup.
the class LangSpecsXmlParser method convertAttributes.
private static List<Attribute> convertAttributes(List<XmlElement> attributeNodes, LanguageSpecificationV2 langSpec) {
List<Attribute> attributes = new ArrayList<>();
for (var attributeNode : attributeNodes) {
Attribute newAttribute = getAttribute(attributeNode, langSpec);
attributes.add(newAttribute);
}
Collections.sort(attributes);
return attributes;
}
use of org.lara.language.specification.dsl.Attribute in project lara-framework by specs-feup.
the class LangSpecsXmlParser method getAttribute.
private static Attribute getAttribute(XmlElement attributeNode, LanguageSpecificationV2 langSpec) {
String type = getType(attributeNode);
Attribute newAttribute = new Attribute(langSpec.getType(type), attributeNode.getAttribute("name"));
setOptional(attributeNode.getAttribute("tooltip"), newAttribute::setToolTip);
var parameterNodes = attributeNode.getElementsByName("parameter");
for (var parameterNode : parameterNodes) {
newAttribute.addParameter(langSpec.getType(getType(parameterNode)), parameterNode.getAttribute("name"));
}
return newAttribute;
}
use of org.lara.language.specification.dsl.Attribute in project lara-framework by specs-feup.
the class WeaverSpecification method attributesOf.
public Object attributesOf(String joinPointName, boolean allInformation) {
JoinPointClass joinPoint = getJoinPoint(joinPointName);
if (joinPoint == null) {
warnMissingJoinPointType(joinPointName, "attributes");
return engine.newNativeArray();
}
Collection<Attribute> attributes;
if (allInformation) {
attributes = joinPoint.getAttributes();
} else {
attributes = joinPoint.getAttributesSelf();
}
List<String> attributeStrings = attributes.stream().map(Attribute::toString).collect(Collectors.toList());
return engine.toNativeArray(attributeStrings);
}
Aggregations