Skip to content

Commit

Permalink
Include dependencyIDs in Service model instead of dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
madushajg committed Jun 28, 2023
1 parent ba0dd17 commit 583b6bf
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public ArchitectureModel constructComponentModel(Package currentPackage, Package
// todo: Change to TypeDefinition
Map<String, Entity> entities = new HashMap<>();
List<ArchitectureModelDiagnostic> diagnostics = new ArrayList<>();
List<Dependency> allDependencies = new ArrayList<>();
AtomicReference<FunctionEntryPoint> functionEntryPoint = new AtomicReference<>();
PackageId packageId = new PackageId(currentPackage);
AtomicBoolean hasDiagnosticErrors = new AtomicBoolean(false);
Expand All @@ -68,7 +69,9 @@ public ArchitectureModel constructComponentModel(Package currentPackage, Package

ServiceModelGenerator serviceModelGenerator = new ServiceModelGenerator(currentPackageCompilation, module);
try {
services.putAll(serviceModelGenerator.generate());
serviceModelGenerator.generate();
services.putAll(serviceModelGenerator.getServices());
allDependencies.addAll(serviceModelGenerator.getDependencies());
} catch (Exception e) {
DiagnosticMessage message = DiagnosticMessage.failedToGenerate(DiagnosticNode.SERVICES,
e.getMessage());
Expand Down Expand Up @@ -98,13 +101,6 @@ public ArchitectureModel constructComponentModel(Package currentPackage, Package
}
});

List<Dependency> allDependencies = new ArrayList<>();

for (Service service : services.values()) {
List<Dependency> dependencies = service.getDependencies();
allDependencies.addAll(dependencies);
}

return new ArchitectureModel(Constants.MODEL_VERSION, packageId, diagnostics, services, entities,
functionEntryPoint.get(), hasDiagnosticErrors.get(), allDependencies);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package io.ballerina.architecturemodelgenerator.core.generators.entrypoint.nodevisitors;

import io.ballerina.architecturemodelgenerator.core.model.common.DisplayAnnotation;
import io.ballerina.architecturemodelgenerator.core.model.common.EntryPointID;
import io.ballerina.architecturemodelgenerator.core.model.service.Dependency;
import io.ballerina.compiler.api.SemanticModel;
import io.ballerina.compiler.api.symbols.ClassSymbol;
Expand Down Expand Up @@ -88,7 +89,8 @@ public void visit(VariableDeclarationNode variableDeclarationNode) {
String serviceId = displayAnnotation.getId() != null ? displayAnnotation.getId() :
Integer.toString(variableDeclarationNode.hashCode());
String serviceLabel = displayAnnotation.getLabel();
Dependency dependency = new Dependency(serviceId, serviceLabel,
EntryPointID entryPointID = new EntryPointID(serviceId, serviceLabel);
Dependency dependency = new Dependency(entryPointID,
getClientModuleName(referredClassSymbol),
getElementLocation(filePath.toString(), variableDeclarationNode.lineRange()),
Collections.emptyList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import io.ballerina.architecturemodelgenerator.core.generators.ModelGenerator;
import io.ballerina.architecturemodelgenerator.core.generators.service.nodevisitors.ServiceDeclarationNodeVisitor;
import io.ballerina.architecturemodelgenerator.core.model.service.Dependency;
import io.ballerina.architecturemodelgenerator.core.model.service.Service;
import io.ballerina.compiler.syntax.tree.SyntaxTree;
import io.ballerina.projects.DocumentId;
Expand All @@ -28,6 +29,8 @@

import java.nio.file.Path;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
Expand All @@ -37,22 +40,32 @@
*/
public class ServiceModelGenerator extends ModelGenerator {

private final Map<String, Service> services = new HashMap<>();
private final List<Dependency> dependencies = new LinkedList<>();

public ServiceModelGenerator(PackageCompilation packageCompilation, Module module) {
super(packageCompilation, module);
}

public Map<String, Service> generate() {
Map<String, Service> services = new HashMap<>();
public Map<String, Service> getServices() {
return services;
}

public List<Dependency> getDependencies() {
return dependencies;
}

public void generate() {
for (DocumentId documentId :getModule().documentIds()) {
SyntaxTree syntaxTree = getModule().document(documentId).syntaxTree();
Path filePath = getModuleRootPath().resolve(syntaxTree.filePath());
ServiceDeclarationNodeVisitor serviceNodeVisitor = new ServiceDeclarationNodeVisitor(
getPackageCompilation(), getSemanticModel(), syntaxTree, getModule().packageInstance(), filePath);
syntaxTree.rootNode().accept(serviceNodeVisitor);
serviceNodeVisitor.getServices().forEach(service -> {
services.put(service.getServiceId(), service);
services.put(service.getServiceId().getId(), service);
});
dependencies.addAll(serviceNodeVisitor.getDependencies());
}
return services;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import io.ballerina.architecturemodelgenerator.core.diagnostics.DiagnosticNode;
import io.ballerina.architecturemodelgenerator.core.generators.GeneratorUtils;
import io.ballerina.architecturemodelgenerator.core.model.common.DisplayAnnotation;
import io.ballerina.architecturemodelgenerator.core.model.common.EntryPointID;
import io.ballerina.architecturemodelgenerator.core.model.service.Dependency;
import io.ballerina.architecturemodelgenerator.core.model.service.Service;
import io.ballerina.compiler.api.SemanticModel;
import io.ballerina.compiler.api.symbols.Symbol;
Expand Down Expand Up @@ -72,6 +74,7 @@ public class ServiceDeclarationNodeVisitor extends NodeVisitor {
private final SyntaxTree syntaxTree;
private final Package currentPackage;
private final List<Service> services = new LinkedList<>();
private final List<Dependency> dependencies = new LinkedList<>();
private final Path filePath;

public ServiceDeclarationNodeVisitor(PackageCompilation packageCompilation, SemanticModel semanticModel,
Expand All @@ -87,6 +90,10 @@ public List<Service> getServices() {
return services;
}

public List<Dependency> getDependencies() {
return dependencies;
}

@Override
public void visit(ServiceDeclarationNode serviceDeclarationNode) {

Expand Down Expand Up @@ -121,12 +128,19 @@ public void visit(ServiceDeclarationNode serviceDeclarationNode) {
);
diagnostics.add(diagnostic);
}
services.add(new Service(serviceName.trim(), serviceAnnotation.getId(),
EntryPointID serviceID = new EntryPointID(serviceAnnotation.getId(), serviceAnnotation.getLabel());
List<EntryPointID> dependencyIDs = new ArrayList<>();

for (Dependency dependency : serviceMemberFunctionNodeVisitor.getDependencies()) {
dependencyIDs.add(dependency.getEntryPointID());
}

services.add(new Service(serviceName.trim(), serviceID,
getServiceType(serviceDeclarationNode), serviceMemberFunctionNodeVisitor.getResources(),
serviceAnnotation, serviceMemberFunctionNodeVisitor.getRemoteFunctions(),
serviceMemberFunctionNodeVisitor.getDependencies(),
serviceAnnotation, serviceMemberFunctionNodeVisitor.getRemoteFunctions(), dependencyIDs,
GeneratorUtils.getElementLocation(filePath.toString(), serviceDeclarationNode.lineRange()),
diagnostics));
dependencies.addAll(serviceMemberFunctionNodeVisitor.getDependencies());
}

private String getServiceType(ServiceDeclarationNode serviceDeclarationNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.ballerina.architecturemodelgenerator.core.diagnostics.DiagnosticNode;
import io.ballerina.architecturemodelgenerator.core.model.ElementLocation;
import io.ballerina.architecturemodelgenerator.core.model.common.DisplayAnnotation;
import io.ballerina.architecturemodelgenerator.core.model.common.EntryPointID;
import io.ballerina.architecturemodelgenerator.core.model.common.FunctionParameter;
import io.ballerina.architecturemodelgenerator.core.model.service.Dependency;
import io.ballerina.architecturemodelgenerator.core.model.service.RemoteFunction;
Expand Down Expand Up @@ -323,7 +324,8 @@ public void visit(ObjectFieldNode objectFieldNode) {
Integer.toString(objectFieldNode.hashCode());
serviceLabel = displayAnnotation.getLabel();
}
Dependency dependency = new Dependency(serviceId, serviceLabel,
EntryPointID entryPointID = new EntryPointID(serviceId, serviceLabel);
Dependency dependency = new Dependency(entryPointID,
getClientModuleName(referredClassSymbol),
getElementLocation(filePath, objectFieldNode.lineRange()), Collections.emptyList());
dependencies.add(dependency);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.ballerina.architecturemodelgenerator.core.model.common;

/**
* Provides entry-point id information.
*
* @since 2201.6.0
*/
public class EntryPointID {

private final String id;
private final String label;

public EntryPointID(String id, String label) {
this.id = id;
this.label = label;
}

public String getId() {
return id;
}

public String getLabel() {
return label;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.ballerina.architecturemodelgenerator.core.diagnostics.ArchitectureModelDiagnostic;
import io.ballerina.architecturemodelgenerator.core.model.ElementLocation;
import io.ballerina.architecturemodelgenerator.core.model.ModelElement;
import io.ballerina.architecturemodelgenerator.core.model.common.EntryPointID;

import java.util.List;

Expand All @@ -30,24 +31,18 @@
* @since 2201.4.0
*/
public class Dependency extends ModelElement {
private final String serviceId;
private final String serviceLabel;
private final EntryPointID entryPointID;
private final String connectorType;

public Dependency(String serviceId, String serviceLabel, String connectorType, ElementLocation elementLocation,
public Dependency(EntryPointID entryPointID, String connectorType, ElementLocation elementLocation,
List<ArchitectureModelDiagnostic> diagnostics) {
super(elementLocation, diagnostics);
this.serviceId = serviceId;
this.serviceLabel = serviceLabel;
this.entryPointID = entryPointID;
this.connectorType = connectorType;
}

public String getServiceId() {
return serviceId;
}

public String getServiceLabel() {
return serviceLabel;
public EntryPointID getEntryPointID() {
return entryPointID;
}

public String getConnectorType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.ballerina.architecturemodelgenerator.core.model.ElementLocation;
import io.ballerina.architecturemodelgenerator.core.model.ModelElement;
import io.ballerina.architecturemodelgenerator.core.model.common.DisplayAnnotation;
import io.ballerina.architecturemodelgenerator.core.model.common.EntryPointID;

import java.util.List;

Expand All @@ -33,15 +34,15 @@
public class Service extends ModelElement {

private final String path;
private final String serviceId;
private final EntryPointID serviceId;
private final String serviceType;
private final List<Resource> resources;
private final DisplayAnnotation annotation;
private final List<RemoteFunction> remoteFunctions;
private final List<Dependency> dependencies;
private final List<EntryPointID> dependencyIDs;

public Service(String path, String serviceId, String serviceType, List<Resource> resources,
DisplayAnnotation annotation, List<RemoteFunction> remoteFunctions, List<Dependency> dependencies,
public Service(String path, EntryPointID serviceId, String serviceType, List<Resource> resources,
DisplayAnnotation annotation, List<RemoteFunction> remoteFunctions, List<EntryPointID> dependencyIDs,
ElementLocation elementLocation, List<ArchitectureModelDiagnostic> diagnostics) {
super(elementLocation, diagnostics);
this.path = path;
Expand All @@ -50,14 +51,14 @@ public Service(String path, String serviceId, String serviceType, List<Resource>
this.resources = resources;
this.annotation = annotation;
this.remoteFunctions = remoteFunctions;
this.dependencies = dependencies;
this.dependencyIDs = dependencyIDs;
}

public String getPath() {
return path;
}

public String getServiceId() {
public EntryPointID getServiceId() {
return serviceId;
}

Expand All @@ -77,7 +78,7 @@ public List<RemoteFunction> getRemoteFunctions() {
return remoteFunctions;
}

public List<Dependency> getDependencies() {
return dependencies;
public List<EntryPointID> getDependencyIDs() {
return dependencyIDs;
}
}

0 comments on commit 583b6bf

Please sign in to comment.