Skip to content
Snippets Groups Projects
Commit ab1274df authored by Jakub Konvička's avatar Jakub Konvička
Browse files

Add parameter addition to fluent API

parent 6ffbdbf6
Branches
Tags
2 merge requests!10Merge: Release version 1.1,!3Implementation of Feature/activity diagram visitor
Showing with 152 additions and 18 deletions
......@@ -64,11 +64,15 @@ namespace MyProgram {
public:
Student(const std::string& name, int studentID) : Person(name), studentID(studentID) {}
void displayInfo() const {
string displayInfo() const {
Person::introduce();
std::cout << "Student ID: " << studentID << std::endl;
}
int to_string() const {
return Person::to_string() + ", " + std::to_string(studentID);
}
private:
int studentID;
};
......
......@@ -20,10 +20,9 @@ public class ClassBuilder : IClassBuilder
return this;
}
public IClassBuilder AddOperation(string name, VisibilityType visibilityType)
public IOperationBuilder AddOperation(string name, PropertyType returnType, VisibilityType visibilityType, string returnTypeValue)
{
_xmiClassDiagramUmlBuilder.AddOperation(_class, name, visibilityType);
return this;
return new OperationBuilder(_xmiClassDiagramUmlBuilder, _class, name, returnType, visibilityType, returnTypeValue);
}
public IClassBuilder AddAssociation(string targetClassName, VisibilityType visibilityType, AssociationType associationType)
......
......@@ -16,12 +16,9 @@ public class ClassDiagramBuilder : IDiagramBuilder
_xmiClassDiagramUmlBuilder = new XmiClassDiagramUmlBuilder(name, xmiBuilder);
}
public IEnumerable<IPackageBuilder> PackageBuilders { get; private set; } = new List<IPackageBuilder>();
public IPackageBuilder AddPackage(string name)
{
var pb = new PackageBuilder(_xmiClassDiagramUmlBuilder, name);
PackageBuilders.Append(pb);
return pb;
}
public string? BuildDiagram()
......
using DiagramBuilder.Builder.Interface;
using DiagramBuilder.Builder.XML;
using DiagramBuilder.Builder.XML.Enum;
namespace DiagramBuilder.Builder.Diagram;
public class OperationBuilder : IOperationBuilder
{
private XmiClassDiagramUmlBuilder _xmiClassDiagramUmlBuilder { get; set; }
private XmiElement _class { get; set; }
private XmiElement _operation { get; set; }
internal OperationBuilder(XmiClassDiagramUmlBuilder xmiClassDiagramUmlBuilder, XmiElement class_, string name, PropertyType returnType, VisibilityType visibilityType, string returnTypeValue)
{
_xmiClassDiagramUmlBuilder = xmiClassDiagramUmlBuilder;
_class = class_;
_operation = _xmiClassDiagramUmlBuilder.AddOperation(_class, name, returnType, visibilityType, returnTypeValue);
}
public IClassBuilder AddProperty(string name, VisibilityType visibilityType, PropertyType propertyType)
{
_xmiClassDiagramUmlBuilder.AddProperty(_class, name, visibilityType, propertyType);
return this;
}
public IOperationBuilder AddOperation(string name, PropertyType returnType, VisibilityType visibilityType, string returnTypeValue)
{
_operation = _xmiClassDiagramUmlBuilder.AddOperation(_class, name, returnType, visibilityType, returnTypeValue);
return this;
}
public IClassBuilder AddAssociation(string targetClassName, VisibilityType visibilityType, AssociationType associationType)
{
_xmiClassDiagramUmlBuilder.AddAssociation(_class, targetClassName, visibilityType, associationType);
return this;
}
public IClassBuilder AddGeneralization(string targetClassName)
{
_xmiClassDiagramUmlBuilder.AddGeneralization(_class, targetClassName);
return this;
}
public IOperationBuilder AddParameter(string name, PropertyType propertyType)
{
_xmiClassDiagramUmlBuilder.AddParameter(_operation, name, propertyType);
return this;
}
}
\ No newline at end of file
......@@ -5,7 +5,7 @@ namespace DiagramBuilder.Builder.Interface;
public interface IClassBuilder
{
IClassBuilder AddProperty(string name, VisibilityType visibilityType, PropertyType propertyType);
IClassBuilder AddOperation(string name, VisibilityType visibilityType);
IOperationBuilder AddOperation(string name, PropertyType returnType, VisibilityType visibilityType, string returnTypeValue);
IClassBuilder AddAssociation(string targetClass, VisibilityType visibilityType, AssociationType associationType);
IClassBuilder AddGeneralization(string targetClass);
}
\ No newline at end of file
using DiagramBuilder.Builder.XML.Enum;
namespace DiagramBuilder.Builder.Interface;
public interface IOperationBuilder : IClassBuilder
{
IOperationBuilder AddParameter(string name, PropertyType propertyType);
}
\ No newline at end of file
......@@ -7,5 +7,6 @@ public enum PropertyType
PrimitiveTypeString,
LiteralInteger,
LiteralBoolean,
NonPrimitiveType
NonPrimitiveType,
NoDataType
}
\ No newline at end of file
......@@ -5,6 +5,7 @@ public enum XmiType
UmlPackage,
UmlClass,
UmlProperty,
UmlParameter,
UmlOperation,
UmlPrimitiveType,
UmlLiteralInteger,
......
......@@ -38,7 +38,7 @@ public class XmiBuilder : XmlBuilder
InitializeDocument(xmlElementDto);
}
public XmiElement AddElement(XmiElement parent, string elementName, XmiType xmiType, string name, List<XmiAttributeDTO>? additionalAttributes)
public XmiElement AddElement(XmiElement parent, string elementName, XmiType xmiType, string name, List<XmiAttributeDTO>? additionalAttributes, bool addAttributeName = true)
{
XmlElementDTO element = new XmlElementDTO()
{
......@@ -60,15 +60,19 @@ public class XmiBuilder : XmlBuilder
? name
: $"{parent?.XmlElement?.GetAttribute("xmi:id")}-{name}",
XmlNamespace = GetNamespace("xmi")
},
new XmiAttributeDTO()
{
AttributeName = "name",
AttributeValue = name,
}
}
};
if (addAttributeName)
{
element.XmlAttributes.Add(new XmiAttributeDTO()
{
AttributeName = "name",
AttributeValue = name,
});
}
if (additionalAttributes != null)
{
element.XmlAttributes.AddRange(additionalAttributes);
......
......@@ -64,7 +64,7 @@ public class XmiClassDiagramUmlBuilder
AddLowerValueAttribute(property, PropertyType.LiteralInteger, null);
}
public void AddOperation(XmiElement parent, string name, VisibilityType visibilityType)
public XmiElement AddOperation(XmiElement parent, string name, PropertyType returnType, VisibilityType visibilityType, string returnTypeValue)
{
//add visibility attribute
var visibilityAttribute = new XmiAttributeDTO()
......@@ -73,7 +73,24 @@ public class XmiClassDiagramUmlBuilder
AttributeValue = Convertor.VisibilityTypeToString(visibilityType)
};
AddOwnedOperation(parent, XmiType.UmlOperation, name, new(){visibilityAttribute});
var operation = AddOwnedOperation(parent, XmiType.UmlOperation, name, new(){visibilityAttribute});
if (returnType != PropertyType.NonPrimitiveType && returnType != PropertyType.NoDataType)
{
var directionAttribute = new XmiAttributeDTO()
{
AttributeName = "direction",
AttributeValue = "return"
};
var parameter = AddOwnedParameter(operation, XmiType.UmlParameter, "returnParameter", new(){ directionAttribute }, false);
var hrefAttribute = new XmiAttributeDTO()
{
AttributeName = "href",
AttributeValue = $"http://www.omg.org/spec/UML/20131001/UML.xmi#{Convertor.PropertyTypeToDataTypeString(returnType)}"
};
AddTypeAttribute(parameter, returnType, new(){ hrefAttribute });
}
return operation;
}
public void AddAssociation(XmiElement parent, string targetClassName, VisibilityType visibilityType, AssociationType associationType)
......@@ -128,6 +145,26 @@ public class XmiClassDiagramUmlBuilder
_nextPackageAssociationId[packageName]++;
}
public void AddParameter(XmiElement parent, string name, PropertyType propertyType)
{
if (propertyType != PropertyType.NonPrimitiveType && propertyType != PropertyType.NoDataType)
{
var directionAttribute = new XmiAttributeDTO()
{
AttributeName = "direction",
AttributeValue = "inout"
};
var parameter = AddOwnedParameter(parent, XmiType.UmlParameter, name, new(){ directionAttribute });
var hrefAttribute = new XmiAttributeDTO()
{
AttributeName = "href",
AttributeValue = $"http://www.omg.org/spec/UML/20131001/UML.xmi#{Convertor.PropertyTypeToDataTypeString(propertyType)}"
};
AddTypeAttribute(parameter, propertyType, new(){ hrefAttribute });
}
}
public void AddGeneralization(XmiElement parentClass, string targetClassName)
......@@ -157,6 +194,11 @@ public class XmiClassDiagramUmlBuilder
{
return _xmiBuilder.AddElement(parent,"ownedOperation", xmiType, name, additionalAttributes);
}
private XmiElement AddOwnedParameter(XmiElement parent, XmiType xmiType, string name, List<XmiAttributeDTO>? additionalAttributes, bool addAttributeName = true)
{
return _xmiBuilder.AddElement(parent,"ownedParameter", xmiType, name, additionalAttributes, addAttributeName);
}
private XmiElement AddPackagedElement(XmiElement parent, XmiType xmiType, string name, List<XmiAttributeDTO>? additionalAttributes)
{
......
......@@ -12,6 +12,7 @@ public class Convertor
XmiType.UmlPackage => $"{prefix}:Package",
XmiType.UmlClass => $"{prefix}:Class",
XmiType.UmlProperty => $"{prefix}:Property",
XmiType.UmlParameter => $"{prefix}:Parameter",
XmiType.UmlOperation => $"{prefix}:Operation",
XmiType.UmlPrimitiveType => $"{prefix}:PrimitiveType",
XmiType.UmlLiteralInteger => $"{prefix}:LiteralInteger",
......@@ -56,6 +57,30 @@ public class Convertor
};
}
public static PropertyType StringToPropertyType(string propertyType)
{
if(string.IsNullOrEmpty(propertyType))
{
return PropertyType.NoDataType;
}
else if (propertyType.Contains("int"))
{
return PropertyType.PrimitiveTypeInteger;
}
else if (propertyType.Contains("bool"))
{
return PropertyType.PrimitiveTypeBoolean;
}
else if (propertyType.Contains("string"))
{
return PropertyType.PrimitiveTypeString;
}
else
{
return PropertyType.NonPrimitiveType;
}
}
public static string PropertyTypeToString(PropertyType propertyType, string prefix)
{
return propertyType switch
......
......@@ -90,7 +90,11 @@ public class CppClassDiagramVisitor : CPP14ParserBaseVisitor<object>
if(member is Function function)
{
var operationBuilder = _classes.Last().Value.AddOperation(function.Name, actualAccessSpecifier);
var operationBuilder = _classes.Last().Value.AddOperation(function.Name, Convertor.StringToPropertyType(function.ReturnType), actualAccessSpecifier, function.ReturnType);
foreach(var param in function.Parameters)
{
operationBuilder.AddParameter(param.Name, Convertor.StringToPropertyType(param.Type));
}
}
else if (member is Atribute atribute)
{
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment