Skip to content
Snippets Groups Projects
Commit bdf3c7c2 authored by Simon Spinner's avatar Simon Spinner
Browse files

Support delegation and requirements for skeletons in generic agent.

parent 19b62860
No related branches found
No related tags found
No related merge requests found
package tools.descartes.prisma.agent.generic; package tools.descartes.prisma.agent.generic;
import java.io.File; import java.io.File;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser; import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options; import org.apache.commons.cli.Options;
import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.common.util.TreeIterator; import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.common.util.URI; import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference; import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.equinox.app.IApplication; import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext; import org.eclipse.equinox.app.IApplicationContext;
...@@ -26,13 +31,14 @@ import edu.kit.ipd.descartes.mm.resourcetype.ResourceType; ...@@ -26,13 +31,14 @@ import edu.kit.ipd.descartes.mm.resourcetype.ResourceType;
import edu.kit.ipd.descartes.mm.resourcetype.ResourceTypeRepository; import edu.kit.ipd.descartes.mm.resourcetype.ResourceTypeRepository;
import edu.kit.ipd.descartes.mm.resourcetype.ResourcetypePackage; import edu.kit.ipd.descartes.mm.resourcetype.ResourcetypePackage;
import tools.descartes.prisma.agent.generic.genericagent.GenericAgentConfiguration; import tools.descartes.prisma.agent.generic.genericagent.GenericAgentConfiguration;
import tools.descartes.prisma.agent.generic.genericagent.RequiredObject;
import tools.descartes.prisma.core.ModelRepository; import tools.descartes.prisma.core.ModelRepository;
import tools.descartes.prisma.core.adapter.ModelSkeletonAdapter; import tools.descartes.prisma.core.adapter.ModelSkeletonAdapter;
import tools.descartes.prisma.core.adapter.ObjectMatcher;
import tools.descartes.prisma.core.agent.AgentController; import tools.descartes.prisma.core.agent.AgentController;
import tools.descartes.prisma.core.agent.DelegationProvider; import tools.descartes.prisma.core.agent.DelegationProvider;
import tools.descartes.prisma.core.impl.CDOModelRepository; import tools.descartes.prisma.core.impl.CDOModelRepository;
import tools.descartes.prisma.model.scope.DelegatedObject; import tools.descartes.prisma.model.scope.DelegatedObject;
import tools.descartes.prisma.model.scope.ScopeConfiguration;
import tools.descartes.prisma.model.skeleton.ModelSkeleton; import tools.descartes.prisma.model.skeleton.ModelSkeleton;
public class GenericAgent implements IApplication { public class GenericAgent implements IApplication {
...@@ -41,19 +47,37 @@ public class GenericAgent implements IApplication { ...@@ -41,19 +47,37 @@ public class GenericAgent implements IApplication {
private ModelSkeleton skeleton; private ModelSkeleton skeleton;
private final Set<EObject> delegatedObjects = new HashSet<>();
public GenericAgentController(ModelRepository repository, GenericAgentConfiguration configuration) { public GenericAgentController(ModelRepository repository, GenericAgentConfiguration configuration) {
super(repository, configuration.getScope().getPath(), configuration.getId()); super(repository, configuration.getScope().getPath(), configuration.getId());
} }
@Override
protected void notifyAddDelegatedElement(EObject newElement, Map<String, String> additionalProperties) {
delegatedObjects.add(newElement);
}
@Override
protected void notifyRemoveDelegatedElement(EObject oldElement, Map<String, String> additionalProperties) {
delegatedObjects.remove(oldElement);
}
public Set<EObject> getDelegatedObjects() {
return delegatedObjects;
}
} }
private class GenericDelegationProvider implements DelegationProvider { private class GenericDelegationProvider implements DelegationProvider {
private final Map<String, Map<String, String>> elements = new HashMap<>(); private final Map<String, Map<String, String>> elements = new HashMap<>();
public GenericDelegationProvider(ScopeConfiguration configuration) { public GenericDelegationProvider(GenericAgentConfiguration configuration) {
for (DelegatedObject curElement : configuration.getDelegatedObjects()) { for (DelegatedObject curElement : configuration.getDelegatedObjects()) {
if (curElement.getObject() instanceof Identifier) { EObject object = curElement.getObject();
elements.put(((Identifier) curElement).getId(), curElement.getAdditionalProperties().map()); if (object instanceof Identifier) {
elements.put(((Identifier) object).getId(), curElement.getAdditionalProperties().map());
} }
} }
} }
...@@ -114,11 +138,16 @@ public class GenericAgent implements IApplication { ...@@ -114,11 +138,16 @@ public class GenericAgent implements IApplication {
ModelRepository repository = new CDOModelRepository(repositoryURL); ModelRepository repository = new CDOModelRepository(repositoryURL);
GenericAgentController agent = new GenericAgentController(repository, configuration); GenericAgentController agent = new GenericAgentController(repository, configuration);
agent.setDelegationProvider(new GenericDelegationProvider(configuration));
agent.start().get(); agent.start().get();
replacePublicResources(agent, configuration.getSkeleton()); replacePublicResources(agent, configuration.getSkeleton());
agent.apply(configuration.getSkeleton()); if (resolveRequiredObjects(agent, configuration)) {
agent.apply(configuration.getSkeleton());
}
agent.stop().get(); agent.stop().get();
...@@ -128,31 +157,27 @@ public class GenericAgent implements IApplication { ...@@ -128,31 +157,27 @@ public class GenericAgent implements IApplication {
@Override @Override
public void stop() { public void stop() {
// TODO Auto-generated method stub // TODO Auto-generated method stub
} }
private GenericAgentConfiguration loadConfiguration(String configurationPath) { private GenericAgentConfiguration loadConfiguration(String configurationPath) {
ResourceSet set = new ResourceSetImpl(); ResourceSet set = new ResourceSetImpl();
Resource res = set.getResource(URI.createFileURI(new File(configurationPath).getAbsolutePath()), true); Resource res = set.getResource(URI.createFileURI(new File(configurationPath).getAbsolutePath()), true);
GenericAgentConfiguration configuration = (GenericAgentConfiguration) res.getContents().get(0); GenericAgentConfiguration configuration = (GenericAgentConfiguration) res.getContents().get(0);
return configuration;
}
private void integrateIntoSkeleton(ModelSkeleton skeleton, EObject root) { // IMPORTANT: resolve all cross-references
for (EReference curReference : skeleton.eClass().getEAllContainments()) { EcoreUtil.resolveAll(set);
if (curReference.getEReferenceType().isSuperTypeOf(root.eClass())) { Map<EObject, Collection<Setting>> unresolved = EcoreUtil.UnresolvedProxyCrossReferencer.find(set);
if (curReference.isMany()) { for (EObject problemObject : unresolved.keySet()) {
@SuppressWarnings("unchecked") Collection<Setting> unresolvedSettings = unresolved.get(problemObject);
List<EObject> values = (List<EObject>) skeleton.eGet(curReference); for (Setting curSetting : unresolvedSettings) {
values.add(root); System.out.println("Unresolved proxy: " + curSetting.getEStructuralFeature());
} else {
skeleton.eSet(curReference, root);
}
return;
} }
} }
return configuration;
} }
private void replacePublicResources(GenericAgentController agent, ModelSkeleton skeleton) { private void replacePublicResources(GenericAgentController agent, ModelSkeleton skeleton) {
// Resource types (e.g., CPU, HDD, DELAY) which are defined globally are // Resource types (e.g., CPU, HDD, DELAY) which are defined globally are
// not included in the model skeleton. // not included in the model skeleton.
...@@ -183,4 +208,30 @@ public class GenericAgent implements IApplication { ...@@ -183,4 +208,30 @@ public class GenericAgent implements IApplication {
} }
} }
private boolean resolveRequiredObjects(GenericAgentController agent, GenericAgentConfiguration configuration) {
Set<RequiredObject> notResolved = new HashSet<>(configuration.getRequiredObjects());
for (EObject curDelegate : agent.getDelegatedObjects()) {
for (RequiredObject reqObject : notResolved) {
if (ObjectMatcher.matches(curDelegate, reqObject.getObject())) {
// Connect the two objects with each other
ModelSkeletonAdapter adapter = agent.getModelRepository().adapt(reqObject,
ModelSkeletonAdapter.class);
adapter.setRepositoryObject((CDOObject) curDelegate);
// Requirement is fulfilled
notResolved.remove(reqObject);
break;
}
}
}
if (!notResolved.isEmpty()) {
for (RequiredObject unresolvedObject : notResolved) {
System.err.println("Could not resolve required object: " + unresolvedObject.getObject());
}
return false;
}
return true;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment