/// <summary> /// Bind a class to a specific convention /// </summary> /// <param name="classType">Type of the class to bind</param> /// <param name="convention">Convention with which the type has to be bound</param> /// <returns>void</returns> private void BindClassToConvention(Type classType, Convention convention) { if (classType == null || convention == null) { return; } List <string> a = (List <string>)_conventionList[convention]; if (!a.Contains(classType.AssemblyQualifiedName)) { a.Add(classType.AssemblyQualifiedName); } }
// Comportamento particolare utilizzato per trovare le convenzioni in caso di chimate a metodi // (proprietà, campi pubbici) statici: se la convenzione non viene trovata per nome della classe // specificato si provano tutte le convenzioni caricate /// <summary> /// Find a convention bound to a specific class providing the name of the class /// </summary> /// <param name="className">Name of the class</param> /// <returns>Convention bound with the specified class or null if it doesn't exist</returns> private Convention FindConventionByClassName(string className) { Convention conv = null; foreach (Convention convention in _conventionList.Keys) { List <string> values = (List <string>)_conventionList[convention]; foreach (Object o in values) { if (o is string) { string t = (string)o; if (t.Equals(className)) { conv = convention; } } } } if (conv == null) { // Convention unknown, then try them all (the first that works) foreach (Convention convention in _conventionList.Keys) { Type cl = convention.GetClass(className); if (cl != null) { return(convention); } } } return(conv); }
private bool oo_get(Term objId, Term memberTerm, Term what) { what = what.getTerm(); // If memberTerm is not an atom and what is a not instantiated variable if (!memberTerm.isAtom()) { return(false); } string memberName = ((Struct)memberTerm).getName(); // Object must be instantiated if (objId is Var) { return(false); } // Retrieving object instance object obj = this.getRegisteredDynamicObject((Struct)objId); if (obj != null) { // Instance property or field Convention conv = FindConvention(obj.GetType()); if (conv != null) { // Convention found string revisedFieldName = conv.GetFieldName(memberName); string revisedPropertyMethod = conv.GetPropertyGetterMethod(memberName); FieldInfo field = obj.GetType().GetField(revisedFieldName); MethodInfo method = obj.GetType().GetMethod(revisedPropertyMethod); if (field != null && method == null) { // Field Struct filedStruct = new Struct(revisedFieldName); Struct objStruct = new Struct(".", objId, filedStruct); Var objVar = new Var(); this.unify(objVar, objStruct); Struct methodStruct = new Struct("get", what); Var methodVar = new Var(); this.unify(methodVar, methodStruct); return(base.java_call_3(objVar, methodVar, new Struct())); } else if (field == null && method != null) { // Property Struct methodStruct = new Struct(revisedPropertyMethod); return(base.java_call_3(objId, methodStruct, what)); } } else { // Convention NOT found FieldInfo field = obj.GetType().GetField(memberName); MethodInfo method = obj.GetType().GetMethod(memberName); if (field != null && method == null) { // Field Struct fieldStruct = new Struct(memberName); Struct objStruct = new Struct(".", objId, fieldStruct); Var objVar = new Var(); this.unify(objVar, objStruct); Struct methodStruct = new Struct("get", what); Var methodVar = new Var(); this.unify(methodVar, methodStruct); return(base.java_call_3(objVar, methodVar, new Struct())); } else if (field == null && method != null) { // Property Struct methodStruct = new Struct(memberName); return(base.java_call_3(objId, methodStruct, what)); } } } else { // Could be a static invocation if (objId.isCompound()) { Struct objIdStruct = (Struct)objId; if (objIdStruct.getArity() == 1 && objIdStruct.getName().Equals("class")) { // Static property or field string className = RemoveApices(objIdStruct.getArg(0).ToString()); Convention conv = FindConventionByClassName(className); if (conv != null) { // Convention found string revisedClassname = conv.GetClassName(className); Type classType = Type.GetType(revisedClassname); string revisedFieldName = conv.GetFieldName(memberName); string revisedPropertyMethod = conv.GetPropertyGetterMethod(memberName); FieldInfo field = classType.GetField(revisedFieldName); MethodInfo method = classType.GetMethod(revisedPropertyMethod); if (field != null && method == null) { // Field Struct fieldStruct = new Struct(revisedFieldName); Struct classNameStruct = new Struct(revisedClassname); Struct classStruct = new Struct("class", classNameStruct); Struct st = new Struct(".", classStruct, fieldStruct); Var classVar = new Var(); this.unify(classVar, st); Struct methodStruct = new Struct("get", what); Var methodVar = new Var(); this.unify(methodVar, methodStruct); return(base.java_call_3(classVar, methodVar, new Struct())); } else if (field == null && method != null) { // Property Struct classNameStruct = new Struct(revisedClassname); Struct classStruct = new Struct("class", classNameStruct); Var classVar = new Var(); this.unify(classVar, classStruct); Struct staticMethod = new Struct(revisedPropertyMethod); Var staticMethodVar = new Var(); this.unify(staticMethodVar, staticMethod); return(base.java_call_3(classVar, staticMethodVar, what)); } } else { // Convention NOT found Type classType = Type.GetType(className); FieldInfo field = classType.GetField(memberName); MethodInfo method = classType.GetMethod(memberName); if (field != null && method == null) { // Field Struct fieldStruct = new Struct(memberName); Struct classNameStruct = new Struct(className); Struct classStruct = new Struct("class", classNameStruct); Struct st = new Struct(".", classStruct, fieldStruct); Var classVar = new Var(); this.unify(classVar, st); Struct methodStruct = new Struct("get", what); Var methodVar = new Var(); this.unify(methodVar, methodStruct); return(base.java_call_3(classVar, methodVar, new Struct())); } else if (field == null && method != null) { // Property Struct classNameStruct = new Struct(className); Struct classStruct = new Struct("class", classNameStruct); Var classVar = new Var(); this.unify(classVar, classStruct); Struct staticMethod = new Struct(memberName); Var staticMethodVar = new Var(); this.unify(staticMethodVar, staticMethod); return(base.java_call_3(classVar, staticMethodVar, what)); } } } } } return(false); }
// In the following three methods is possible to factorize the code // Check this!!! public bool method_call_3(Term objId, Term method_name, Term idResult) { Struct objIdTerm = (Struct)objId.getTerm(); Struct methodNameStruct = (Struct)method_name.getTerm(); string methodName = methodNameStruct.getName(); // check for accessing field or property Obj.Field <- set/get(X) // in that case: objId is '.'(Obj, Field) if (!objIdTerm.isAtom()) { if (objIdTerm is Var) { return(false); } Struct objIdStruct = (Struct)objIdTerm; if (objIdStruct.getName().Equals(".") && objIdStruct.getArity() == 2 && methodNameStruct.getArity() == 1) { if (methodName.Equals("set")) { return(oo_set(objIdStruct.getTerm(0), objIdStruct.getTerm(1), methodNameStruct.getTerm(0))); } else if (methodName.Equals("get")) { return(oo_get(objIdStruct.getTerm(0), objIdStruct.getTerm(1), methodNameStruct.getTerm(0))); } } } // Object must be instantiated if (objIdTerm is Var) { return(false); } // Retrieving object instance object obj = this.getRegisteredDynamicObject((Struct)objIdTerm); if (obj != null) { // Instance method Convention conv = FindConvention(obj.GetType()); if (conv != null) { // Convention found string revisedMethodName = conv.GetMemberName(methodName); Struct methodStruct = new Struct(revisedMethodName, getArrayFromMethod(methodNameStruct)); return(base.java_call_3(objId, methodStruct, idResult)); } else { // Convention NOT found return(base.java_call_3(objId, method_name, idResult)); } } else { // Object not found: could be a static call if (objId.isCompound()) { if (objIdTerm.getArity() == 1 && objIdTerm.getName().Equals("class")) { // Static method string className = RemoveApices(objIdTerm.getArg(0).ToString()); Convention conv = FindConventionByClassName(className); if (conv != null) { // Convention found string revisedClassName = conv.GetClassName(className); string revisedMethodName = conv.GetMemberName(methodName); Struct classNameStruct = new Struct(revisedClassName); Struct classStruct = new Struct("class", classNameStruct); Var classVar = new Var(); this.unify(classVar, classStruct); Struct staticMethod = new Struct(revisedMethodName, getArrayFromMethod(methodNameStruct)); Var staticMethodVar = new Var(); this.unify(staticMethodVar, staticMethod); return(base.java_call_3(classVar, staticMethodVar, idResult)); } else { // Convention NOT found return(base.java_call_3(objId, method_name, idResult)); } } } } return(false); }
/// <summary> /// Creates of a .NET object from an assembly - not backtrackable case /// </summary> /// <param name="conventionName">Name of the convention to bind with the object</param> /// <param name="className">Name of the class with namespace informations</param> /// <param name="constructorName">Name of the constructor used to instantiate the class</param> /// <param name="argl">List of arguments for the constructor</param> /// <param name="id">Reference to the instantiated object (out)</param> /// <returns>True if the operation is successful</returns> public bool new_object_5(Term conventionName, Term className, Term constructorName, Term argl, Term id) { className = className.getTerm(); Struct arg = (Struct)argl.getTerm(); id = id.getTerm(); // Class name must be a simple atom if (!className.isAtom()) { return(false); } string clName = ((Struct)className).getName(); // Retrieving Convention name if present string convName = null; if (conventionName != null && (conventionName.getTerm() is Struct)) { convName = ((Struct)conventionName.getTerm()).getName(); } // Retrieving Constructor name if present string constrName = null; if (constructorName != null && (constructorName.getTerm() is Struct)) { constrName = ((Struct)constructorName.getTerm()).getName(); } Convention conv = FindConvention(convName); if (conv != null) { // Convention found Struct revisedClassNameStruct = null; string revisedClassName = conv.GetClassName(clName); revisedClassNameStruct = new Struct(revisedClassName); if (conv.IsArrayClass(revisedClassName)) { // Construction of an array string arrayClassName = revisedClassName.Substring(0, revisedClassName.Length - 2); arrayClassName = arrayClassName + "[]"; revisedClassNameStruct = new Struct(arrayClassName); } if (constrName != null && !constrName.Equals(String.Empty)) { // If it is specified the static method that build the object, I have to call it string revisedConstrName = conv.GetConstructorName(constrName); string constrClassName = conv.GetConstructorClass(revisedClassName); Struct constrNameStruct = new Struct(constrClassName); Struct constr = new Struct("class", constrNameStruct); Var constrClass = new Var(); this.unify(constrClass, constr); Struct constrMethod = new Struct(revisedConstrName, getArrayFromList(arg)); Var constrMethodName = new Var(); this.unify(constrMethodName, constrMethod); return(base.java_call_3(constrClass, constrMethodName, id)); } // Call the super class method after the modification of the names bool returnValue = base.java_object_3(revisedClassNameStruct, argl, id); // If everything went fine, the type has been loaded correctly by the JavaLibrary class loader // therefore we can bind the type to the convention if (returnValue) { Type classType = Type.GetType(revisedClassName); if (!IsBoundToConvention(classType)) { // The class is not link to the convention: link it! BindClassToConvention(classType, conv); } } return(returnValue); } else { // Convention NOT found // For arrays nothing to do: I guess that the user used [] if (constrName != null && !constrName.Equals(String.Empty)) { // If it is specified the static method that build the object, I have to call it // but this time without convention Struct constrNameStruct = new Struct(clName); Struct constr = new Struct("class", constrNameStruct); Var constrClass = new Var(); this.unify(constrClass, constr); Struct constrMethod = new Struct(constrName, getArrayFromList(arg)); Var constrMethodName = new Var(); this.unify(constrMethodName, constrMethod); return(base.java_call_3(constrClass, constrMethodName, id)); } } // No modifications return(base.java_object_3(className, argl, id)); }
/// <summary> /// Load a convention by its assembly and classname /// </summary> /// <param name="assemblyName">Assembly file that contains the convention</param> /// <param name="conventionName">Class name of the convention</param> /// <param name="id">Identifier of the convention</param> /// <returns>True if the operation is successful</returns> public bool load_convention_3(Term assemblyName, Term conventionName, Term id) { Struct convName = (Struct)conventionName.getTerm(); Struct asName = (Struct)assemblyName.getTerm(); if (convName == null || !convName.isAtom() || asName == null || !asName.isAtom() || (!(id is Var) && !id.isAtom())) { return(false); } try { Convention newConv = Convention.LoadConvention(asName.getName(), convName.getName()); Convention alreadyPresent = FindConvention(newConv.Name); // Convention not loaded if (alreadyPresent == null) { _conventionList.Add(newConv, new List <string>()); // Id is an atom or a variable bound to an atom if (id.isAtom() || id.getTerm().isAtom()) { // Check if the id specified is already in use if (this.getRegisteredObject((Struct)id) == null) { return(this.bindDynamicObject(id.getTerm(), newConv)); } else { return(false); } } else if (id is Var) { // Id set to Convention.Name and unified with the variable Struct newId = new Struct(newConv.Name); return((this.bindDynamicObject(newId, newConv)) && (this.unify(id, newId))); } } else { // Convention already loaded if (id.isAtom() || id.getTerm().isAtom()) { string key = RemoveApices(id.getTerm().ToString()); object conv = this.getRegisteredDynamicObject((Struct)id); if (conv != null && conv is Convention) { Convention found = (Convention)conv; // If convention found in objects and in conventions lists are equals, ok! if (found.Name.Equals(alreadyPresent.Name)) { return(true); } else { return(false); } } else { return(false); } } else { // TO CHECK ----------------------------------------------------- // If id is var then find the atom identifier and bind to id // Here the Convention is already loaded so (in theory) the following call will return the identifier // without register the Convention another time, however this call could be unsafe Struct oldId = this.registerDynamic(alreadyPresent); return(this.unify(id, oldId)); #region Albertin Code //if (_currentObjects_inverse.Contains(alreadyPresent)) // return Unify(id, (Struct)_currentObjects_inverse[alreadyPresent]); //else // return false; #endregion } } return(false); } catch (Exception ex) { return(false); } }