m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java
diff options
context:
space:
mode:
authorMartin <marcin.j.chrzanowski@gmail.com>2019-11-19 12:53:14 +0100
committerGitHub <noreply@github.com>2019-11-19 12:53:14 +0100
commit11df9be19067a6567b499d37cba07e41d21b3c52 (patch)
treeb7b4887ce1f44a805f93b113939c29dcb6d39c69 /src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java
parent3de1798d35334b7bad2a1c9d49ca4a50f2762bf4 (diff)
Run queries (#19)
Diffstat (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java')
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java42
1 files changed, 41 insertions, 1 deletions
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java
index 12682a6..4ac6f5c 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java
@@ -1,10 +1,18 @@
package pl.edu.mimuw.cloudatlas.agent;
+import java.io.PrintStream;
+
import java.rmi.RemoteException;
-import java.util.Set;
+import java.util.List;
import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import pl.edu.mimuw.cloudatlas.interpreter.Interpreter;
+import pl.edu.mimuw.cloudatlas.interpreter.InterpreterException;
+import pl.edu.mimuw.cloudatlas.interpreter.Main;
+import pl.edu.mimuw.cloudatlas.interpreter.QueryResult;
import pl.edu.mimuw.cloudatlas.model.Attribute;
import pl.edu.mimuw.cloudatlas.model.AttributesMap;
import pl.edu.mimuw.cloudatlas.model.PathName;
@@ -13,6 +21,7 @@ import pl.edu.mimuw.cloudatlas.model.Value;
import pl.edu.mimuw.cloudatlas.model.ValueQuery;
import pl.edu.mimuw.cloudatlas.model.ValueSet;
import pl.edu.mimuw.cloudatlas.model.ValueNull;
+import pl.edu.mimuw.cloudatlas.model.Type;
import pl.edu.mimuw.cloudatlas.model.TypePrimitive;
import pl.edu.mimuw.cloudatlas.model.ZMI;
import pl.edu.mimuw.cloudatlas.api.Api;
@@ -53,6 +62,7 @@ public class ApiImplementation implements Api {
ValueQuery query = new ValueQuery(queryCode);
Attribute attributeName = new Attribute(name);
installQueryInHierarchy(root, attributeName, query);
+ executeAllQueries(root);
} catch (Exception e) {
throw new RemoteException("Failed to install query", e);
}
@@ -84,11 +94,41 @@ public class ApiImplementation implements Api {
try {
ZMI zmi = root.findDescendant(new PathName(zoneName));
zmi.getAttributes().addOrChange(new Attribute(attributeName), value);
+ executeAllQueries(root);
} catch (ZMI.NoSuchZoneException e) {
throw new RemoteException("Zone not found", e);
}
}
+ private void executeAllQueries(ZMI zmi) {
+ if(!zmi.getSons().isEmpty()) {
+ for(ZMI son : zmi.getSons()) {
+ executeAllQueries(son);
+ }
+
+ Interpreter interpreter = new Interpreter(zmi);
+ for (ValueQuery query : getQueries(zmi)) {
+ try {
+ List<QueryResult> result = interpreter.interpretProgram(query.getQuery());
+ for(QueryResult r : result) {
+ zmi.getAttributes().addOrChange(r.getName(), r.getValue());
+ }
+ } catch(InterpreterException exception) {}
+ }
+ }
+ }
+
+ private Set<ValueQuery> getQueries(ZMI zmi) {
+ Set<ValueQuery> querySet = new HashSet<ValueQuery>();
+ for (Map.Entry<Attribute, Value> attribute : zmi.getAttributes()) {
+ if (attribute.getValue().getType().getPrimaryType() == Type.PrimaryType.QUERY) {
+ querySet.add((ValueQuery) attribute.getValue());
+ }
+ }
+
+ return querySet;
+ }
+
public void setFallbackContacts(Set<ValueContact> contacts) throws RemoteException {
this.contacts = contacts;
}