X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=iotjava%2Fiotpolicy%2Ftree%2FInterfaceDecl.java;h=30dd3ce002804cb6e0b84a3f9c4f10624416038a;hb=f972344b2c1afa4169a61f7e61ce9a051d0f48a6;hp=27e932615d7c40776e560dd5167e675811b20902;hpb=f0af28e363e1e1d2a3f98eb5bacde13ad09eef8b;p=iot2.git diff --git a/iotjava/iotpolicy/tree/InterfaceDecl.java b/iotjava/iotpolicy/tree/InterfaceDecl.java index 27e9326..30dd3ce 100644 --- a/iotjava/iotpolicy/tree/InterfaceDecl.java +++ b/iotjava/iotpolicy/tree/InterfaceDecl.java @@ -1,7 +1,11 @@ package iotpolicy.tree; import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Set; /** Class InterfaceDecl is a data structure for interface * declaration section in the policy file. @@ -10,12 +14,7 @@ import java.util.List; * @version 1.0 * @since 2016-09-20 */ -public final class InterfaceDecl { - - /** - * Class properties - */ - private String origInt; +public class InterfaceDecl extends Declaration { /** * A "interface" statement: @@ -30,40 +29,53 @@ public final class InterfaceDecl { * In this data structure we will record its interface name, i.e. Camera * its method names and the parameters for each method. */ - private List listMethods; // Method names, e.g. MethodA + + /** + * Class properties + */ + private List listMethods; // Method signature (no spaces), e.g. MethodA(intA,SpeakerB) + private List listMethodIds; // Method identifiers, e.g. MethodA private List listMethodTypes; // Method types, e.g. void private List> listMethodParams; // Method parameter names, e.g. A, B private List> listMethodParamTypes; // Method parameter types, e.g. int, int + private Map mapHelperNumMethodId; // Helper method Id, e.g. for callbacks, structs. + + private static int helperMethodIdNum = -9999; /** * Class constructors */ public InterfaceDecl() { - origInt = null; + super(); listMethods = new ArrayList(); + listMethodIds = new ArrayList(); listMethodTypes = new ArrayList(); listMethodParams = new ArrayList>(); listMethodParamTypes = new ArrayList>(); + mapHelperNumMethodId = new HashMap(); } public InterfaceDecl(String _origInt) { - origInt = _origInt; + super(_origInt); listMethods = new ArrayList(); + listMethodIds = new ArrayList(); listMethodTypes = new ArrayList(); listMethodParams = new ArrayList>(); listMethodParamTypes = new ArrayList>(); + mapHelperNumMethodId = new HashMap(); } /** * addNewMethod() adds a new method name and type into the list */ - public void addNewMethod(String newMethod, String newMethodType) { + public void addNewMethod(String newMethod, String newMethodId, String newMethodType) { listMethods.add(newMethod); + listMethodIds.add(newMethodId); listMethodTypes.add(newMethodType); listMethodParams.add(new ArrayList()); listMethodParamTypes.add(new ArrayList()); @@ -90,8 +102,40 @@ public final class InterfaceDecl { return listMethods; } - - + + + /** + * getMethodNumId() gets Id number for a method + */ + public int getMethodNumId(String method) { + + return listMethods.indexOf(method); + } + + + /** + * getHelperMethodNumId() gets Id number for a method + */ + public int getHelperMethodNumId(String method) { + + if (!mapHelperNumMethodId.containsKey(method)) { + mapHelperNumMethodId.put(method, helperMethodIdNum++); + return mapHelperNumMethodId.get(method); + } else { + return mapHelperNumMethodId.get(method); + } + } + + + /** + * getMethodIds() gets method identifiers + */ + public List getMethodIds() { + + return listMethodIds; + } + + /** * getMethodTypes() gets method types */ @@ -101,12 +145,36 @@ public final class InterfaceDecl { } + /** + * getMethodId() gets a method identifier + */ + public String getMethodId(String method) { + + int index = listMethods.indexOf(method); + // If index=-1, it means that it's not found. + // There is perhaps a discrepancy in the policy file + // between the method signatures in the interface + // and capability sections + if (index == -1) + throw new Error("InterfaceDecl: Discrepancies in method signature for " + + method + "! Please check your policy file..."); + return listMethodIds.get(index); + } + + /** * getMethodType() gets a method type */ public String getMethodType(String method) { int index = listMethods.indexOf(method); + // If index=-1, it means that it's not found. + // There is perhaps a discrepancy in the policy file + // between the method signatures in the interface + // and capability sections + if (index == -1) + throw new Error("InterfaceDecl: Discrepancies in method signature for " + + method + "! Please check your policy file..."); return listMethodTypes.get(index); } @@ -117,6 +185,13 @@ public final class InterfaceDecl { public List getMethodParams(String method) { int index = listMethods.indexOf(method); + // If index=-1, it means that it's not found. + // There is perhaps a discrepancy in the policy file + // between the method signatures in the interface + // and capability sections + if (index == -1) + throw new Error("InterfaceDecl: Discrepancies in method signature for " + + method + "! Please check your policy file..."); return listMethodParams.get(index); } @@ -127,31 +202,13 @@ public final class InterfaceDecl { public List getMethodParamTypes(String method) { int index = listMethods.indexOf(method); + // If index=-1, it means that it's not found. + // There is perhaps a discrepancy in the policy file + // between the method signatures in the interface + // and capability sections + if (index == -1) + throw new Error("InterfaceDecl: Discrepancies in method signature for " + + method + "! Please check your policy file..."); return listMethodParamTypes.get(index); } - - - public static void main(String[] args) { - - InterfaceDecl id = new InterfaceDecl("Camera"); - id.addNewMethod("MethodA", "void"); - id.addNewMethod("MethodB", "int"); - id.addNewMethod("MethodC", "String"); - id.addMethodParam("MethodA", "A", "int"); - id.addMethodParam("MethodA", "B", "int"); - id.addMethodParam("MethodB", "C", "int"); - id.addMethodParam("MethodB", "D", "string"); - id.addMethodParam("MethodC", "E", "string"); - id.addMethodParam("MethodC", "F", "int"); - - - - System.out.println("Set of methods: " + id.getMethods().toString()); - System.out.println("Set of params: " + id.getMethodParams("MethodA").toString()); - System.out.println("Set of paramtypes: " + id.getMethodParamTypes("MethodA").toString()); - System.out.println("Set of params: " + id.getMethodParams("MethodB").toString()); - System.out.println("Set of paramtypes: " + id.getMethodParamTypes("MethodB").toString()); - System.out.println("Set of params: " + id.getMethodParams("MethodC").toString()); - System.out.println("Set of paramtypes: " + id.getMethodParamTypes("MethodC").toString()); - } }