diff options
author | Martin <marcin.j.chrzanowski@gmail.com> | 2019-11-19 12:53:14 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-19 12:53:14 +0100 |
commit | 11df9be19067a6567b499d37cba07e41d21b3c52 (patch) | |
tree | b7b4887ce1f44a805f93b113939c29dcb6d39c69 /src/main/java/pl/edu/mimuw/cloudatlas | |
parent | 3de1798d35334b7bad2a1c9d49ca4a50f2762bf4 (diff) |
Run queries (#19)
Diffstat (limited to 'src/main/java/pl/edu/mimuw/cloudatlas')
4 files changed, 68 insertions, 7 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; } diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/NoopStream.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/NoopStream.java new file mode 100644 index 0000000..cbd4b91 --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/NoopStream.java @@ -0,0 +1,7 @@ +package pl.edu.mimuw.cloudatlas.agent; + +import java.io.OutputStream; + +class NoopOutputStream extends OutputStream { + public void write(int b) {} +} diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Main.java b/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Main.java index 979aa92..4d54bb3 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Main.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/Main.java @@ -35,6 +35,7 @@ import java.util.HashSet; import java.util.List; import java.util.Scanner; +import pl.edu.mimuw.cloudatlas.interpreter.query.Absyn.Program; import pl.edu.mimuw.cloudatlas.interpreter.query.Yylex; import pl.edu.mimuw.cloudatlas.interpreter.query.parser; import pl.edu.mimuw.cloudatlas.model.PathName; @@ -60,19 +61,26 @@ public class Main { Scanner scanner = new Scanner(in); scanner.useDelimiter("\\n"); while(scanner.hasNext()) { - executeQueries(root, scanner.next(), out); + executeQueriesFromSource(root, scanner.next(), out); } scanner.close(); } - private static void executeQueries(ZMI zmi, String query, PrintStream out) throws Exception { + private static void executeQueriesFromSource(ZMI zmi, String query, PrintStream out) throws Exception { + Yylex lex = new Yylex(new ByteArrayInputStream(query.getBytes())); + Program program = (new parser(lex)).pProgram(); + executeQueries(zmi, program, out); + } + + public static void executeQueries(ZMI zmi, Program program, PrintStream out) throws Exception { if(!zmi.getSons().isEmpty()) { - for(ZMI son : zmi.getSons()) - executeQueries(son, query, out); + for(ZMI son : zmi.getSons()) { + executeQueries(son, program, out); + } + Interpreter interpreter = new Interpreter(zmi); - Yylex lex = new Yylex(new ByteArrayInputStream(query.getBytes())); try { - List<QueryResult> result = interpreter.interpretProgram((new parser(lex)).pProgram()); + List<QueryResult> result = interpreter.interpretProgram(program); PathName zone = zmi.getPathName(); for(QueryResult r : result) { out.println(zone + ": " + r); @@ -303,6 +311,7 @@ public class Main { violet07.getAttributes().add("creation", new ValueTime("2011/11/09 20:8:13.123")); violet07.getAttributes().add("cpu_usage", new ValueDouble(0.9)); violet07.getAttributes().add("num_cores", new ValueInt(3l)); + violet07.getAttributes().add("num_processes", new ValueInt(131l)); violet07.getAttributes().add("has_ups", new ValueBoolean(null)); list = Arrays.asList(new Value[] { new ValueString("tola"), new ValueString("tosia"), @@ -334,6 +343,7 @@ public class Main { khaki31.getAttributes().add("creation", new ValueTime("2011/11/09 20:12:13.123")); khaki31.getAttributes().add("cpu_usage", new ValueDouble(null)); khaki31.getAttributes().add("num_cores", new ValueInt(3l)); + khaki31.getAttributes().add("num_processes", new ValueInt(124l)); khaki31.getAttributes().add("has_ups", new ValueBoolean(false)); list = Arrays.asList(new Value[] { new ValueString("agatka"), new ValueString("beatka"), new ValueString("celina"), diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java index d9cbe4c..82e1602 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueQuery.java @@ -32,6 +32,10 @@ public class ValueQuery extends Value { this.query = null; } + public Program getQuery() { + return query; + } + @Override public Type getType() { return TypePrimitive.QUERY; |