/** * \param blockInfo the blockInfo that should be translated * \param permutation the input permutation * \param outputElements will contain the orginal elements * \param outputConstraints will contain the orginal constraints * */ public static void translateFromBlocks(TranslateInfo blockInfo, List<int> permutation, List<int> outputElements/*, List<PartialOrderedSet.NetworkAlgorithm2.Constraint> outputConstraints*/) { outputElements.Clear(); foreach( int blockIndex in permutation ) { outputElements.AddRange(blockInfo.blocks[blockIndex].orginalElements); } }
public MacroVar GetMacro(Scope objectScope, Scope staticScope, MacroVarDeclaration macroContext) { // Get the return type. CodeType returnType = CodeType.GetCodeTypeFromContext(this, macroContext.Type); MacroVar newMacro = new MacroVar(this, objectScope, staticScope, macroContext, returnType); TranslateInfo.ApplyBlock((IApplyBlock)newMacro); return(newMacro); }
/// <summary>Creates a macro from a Define_macroContext.</summary> /// <param name="objectScope">The scope of the macro if there is no static attribute.</param> /// <param name="staticScope">The scope of the macro if there is a static attribute.</param> /// <param name="macroContext">The context of the macro.</param> /// <returns>A DefinedMacro if the macro has parameters, a MacroVar if there are no parameters.</returns> public DefinedMacro GetMacro(Scope objectScope, Scope staticScope, MacroFunctionContext macroContext) { // Get the return type. CodeType returnType = CodeType.GetCodeTypeFromContext(this, macroContext.Type); DefinedMacro newMacro = new DefinedMacro(this, objectScope, staticScope, macroContext, returnType); TranslateInfo.ApplyBlock((IApplyBlock)newMacro); return(newMacro); }
public TranslateAction(IList <EVertex> vertices, IList <Vector2> begin, IList <Vector2> end, HistorySystem s) : base(s) { translates = new TranslateInfo[vertices.Count]; for (int i = 0; i < vertices.Count; i++) { translates[i] = new TranslateInfo(vertices[i], begin[i], end[i]); } }
/** * tries to group the chained elements into blocks * * the elements must be in a valid order allready * * this function also translates all constraints into the blocks of TranslateInfo * */ public static TranslateInfo translateIntoBlocks(List<int> elements, List<CausalSets.Constraint> constraints) { int i; int elementI; int blockIdCounter; bool blockIdCounterWasIncremented; TranslateInfo resultTranslateInfo; ElementInfo[] elementInfos; debugInput(elements, constraints); resultTranslateInfo = new TranslateInfo(); elementInfos = new ElementInfo[elements.Count]; for( i = 0; i < elements.Count; i++ ) { elementInfos[i] = new ElementInfo(); } // go througth all the constraints and search the index of the dependencies and the element then store it in the array storeDependenciesIntoElementInfos(elements, elementInfos, constraints); // search in the array for interlinked blocks, label the elements with a blockcounter blockIdCounter = 0; blockIdCounterWasIncremented = false; for( elementI = 1; elementI < elements.Count; elementI++ ) { if( elementInfos[elementI].dependencies.Count > 0 && elementInfos[elementI].dependencies.Contains(elementI-1) ) { elementInfos[elementI].blockId = blockIdCounter; } else { blockIdCounter++; elementInfos[elementI].blockId = blockIdCounter; } } // store the elements into the blocks // and transfer the orginal constraints to the blocks storeElementsIntoBlocks(elements, elementInfos, resultTranslateInfo); checkInvariantForBlocks(elements, resultTranslateInfo); transferConstraintsToBlocks(resultTranslateInfo, constraints); debugResultTranslateInfo(resultTranslateInfo); return resultTranslateInfo; }
/// <summary>Creates a macro from a Define_macroContext.</summary> /// <param name="objectScope">The scope of the macro if there is no static attribute.</param> /// <param name="staticScope">The scope of the macro if there is a static attribute.</param> /// <param name="macroContext">The context of the macro.</param> /// <returns>A DefinedMacro if the macro has parameters, a MacroVar if there are no parameters.</returns> public IScopeable GetMacro(Scope objectScope, Scope staticScope, DeltinScriptParser.Define_macroContext macroContext) { // If the ; is missing, syntax error. if (macroContext.STATEMENT_END() == null) { Script.Diagnostics.Error("Expected ;", DocRange.GetRange((object)macroContext.TERNARY_ELSE() ?? (object)macroContext.name ?? (object)macroContext).end.ToRange()); } // If the : is missing, syntax error. if (macroContext.TERNARY_ELSE() == null) { Script.Diagnostics.Error("Expected :", DocRange.GetRange(macroContext).end.ToRange()); } else { // Get the expression that will be parsed. if (macroContext.expr() == null) { Script.Diagnostics.Error("Expected expression.", DocRange.GetRange(macroContext.TERNARY_ELSE())); } } // Get the return type. CodeType returnType = CodeType.GetCodeTypeFromContext(this, macroContext.code_type()); IScopeable newMacro; if (macroContext.LEFT_PAREN() != null || macroContext.RIGHT_PAREN() != null) { newMacro = new DefinedMacro(this, objectScope, staticScope, macroContext, returnType); } else { newMacro = new MacroVar(this, objectScope, staticScope, macroContext, returnType); } TranslateInfo.ApplyBlock((IApplyBlock)newMacro); return(newMacro); }
protected void AddLocalizingDefStrings(string localizerType, IEnumerable <Tuple <string, string> > items) { Dictionary <string, TranslateInfo> localizerItems; if (!__Translates.TryGetValue(localizerType, out localizerItems)) { localizerItems = new Dictionary <string, TranslateInfo>(); __Translates.Add(localizerType, localizerItems); } foreach (var item in items) { string idCode = item.Item1; string defString = item.Item2; if (!localizerItems.TryGetValue(idCode, out var translateInfo)) { translateInfo = new TranslateInfo() { IdCode = idCode, Localized = null, DefString = defString }; localizerItems.Add(idCode, translateInfo); } } }
/** \brief checks the invariant, that the order of the elements must be preserved in the result * */ private static void checkInvariantForBlocks(List<int> elements, TranslateInfo resultTranslateInfo) { if(true) { int elementI; int blockIndex; int indexOfElementInBlock; blockIndex = 0; indexOfElementInBlock = 0; for( elementI = 0; elementI < elements.Count; elementI++ ) { int orginalElement; int translatedElement; orginalElement = elements[elementI]; System.Diagnostics.Debug.Assert(resultTranslateInfo.blocks.Count > blockIndex, "Too few blocks!"); translatedElement = resultTranslateInfo.blocks[blockIndex].orginalElements[indexOfElementInBlock]; indexOfElementInBlock++; if( indexOfElementInBlock >= resultTranslateInfo.blocks[blockIndex].orginalElements.Count ) { blockIndex++; indexOfElementInBlock = 0; } System.Diagnostics.Debug.Assert(orginalElement == translatedElement); } } }
/** * links the blocks together after the old linkage information if the orginal elements * * */ private static void transferConstraintsToBlocks(TranslateInfo resultTranslateInfo, List<CausalSets.Constraint> constraints) { foreach( CausalSets.Constraint iterationConstraint in constraints ) { int posteriorBlockIndex; int posteriorBlockElementIndex; int preteriorBlockIndex; int preteriorBlockElementIndex; // for debugging bool preteriorBlockFound; bool posteriorBlockFound; preteriorBlockFound = false; posteriorBlockFound = false; // search block where the posterior is contained for( posteriorBlockIndex = 0; posteriorBlockIndex < resultTranslateInfo.blocks.Count; posteriorBlockIndex++ ) { bool found; found = false; for( posteriorBlockElementIndex = 0; posteriorBlockElementIndex < resultTranslateInfo.blocks[posteriorBlockIndex].orginalElements.Count; posteriorBlockElementIndex++ ) { int elementInBlock; elementInBlock = resultTranslateInfo.blocks[posteriorBlockIndex].orginalElements[posteriorBlockElementIndex]; if( elementInBlock == iterationConstraint.postierElement ) { found = true; posteriorBlockFound = true; break; } } if( found ) { break; } } System.Diagnostics.Debug.Assert(posteriorBlockFound); // the same for the preterior for (preteriorBlockIndex = 0; preteriorBlockIndex < resultTranslateInfo.blocks.Count; preteriorBlockIndex++) { bool found; found = false; for (preteriorBlockElementIndex = 0; preteriorBlockElementIndex < resultTranslateInfo.blocks[preteriorBlockIndex].orginalElements.Count; preteriorBlockElementIndex++) { int elementInBlock; elementInBlock = resultTranslateInfo.blocks[preteriorBlockIndex].orginalElements[preteriorBlockElementIndex]; if (elementInBlock == iterationConstraint.preiorElement ) { found = true; preteriorBlockFound = true; break; } } if( found ) { break; } } System.Diagnostics.Debug.Assert(preteriorBlockFound); if( posteriorBlockIndex != preteriorBlockIndex ) { resultTranslateInfo.blocks[posteriorBlockIndex].dependencyBlockIndices.Add(preteriorBlockIndex); } } }
/** * * stores the elements into the coresponding blocks * a block can contain at least one element (from the orginal) * */ private static void storeElementsIntoBlocks(List<int> elements, ElementInfo[] elementInfos, TranslateInfo resultTranslateInfo) { // go element for element and handle blocks like elements int elementI; int lastBlockId; lastBlockId = -1; for( elementI = 0; elementI < elements.Count; elementI++ ) { if( elementInfos[elementI].blockId != -1 ) { // NOTE< element can't be at the beginning, so we don't need to test for this boundary condition that this is the first element > if( elementInfos[elementI].blockId != lastBlockId ) { TranslateInfo.BlockInfo newBlockInfo; lastBlockId = elementInfos[elementI].blockId; // add new block newBlockInfo = new TranslateInfo.BlockInfo(); newBlockInfo.orginalElements.Add(elements[elementI]); resultTranslateInfo.blocks.Add(newBlockInfo); } else { // add to last block resultTranslateInfo.blocks[resultTranslateInfo.blocks.Count - 1].orginalElements.Add(elements[elementI]); } } else { TranslateInfo.BlockInfo newBlockInfo; // reset because we are no more in an block lastBlockId = -1; newBlockInfo = new TranslateInfo.BlockInfo(); newBlockInfo.orginalElements.Add(elements[elementI]); resultTranslateInfo.blocks.Add(newBlockInfo); } } }
private static void debugResultTranslateInfo(TranslateInfo resultTranslateInfo) { if(false) { int blockCounter; System.Console.WriteLine("BlockResponsibility Result"); System.Console.WriteLine(" blocks"); blockCounter = 0; foreach( TranslateInfo.BlockInfo iterationBlock in resultTranslateInfo.blocks ) { System.Console.WriteLine(" ({0})", blockCounter); System.Console.WriteLine(" orginal elements"); foreach( int element in iterationBlock.orginalElements ) { System.Console.Write("{0} ", element); } System.Console.WriteLine(); System.Console.WriteLine(" block dependencies"); foreach( int dependency in iterationBlock.dependencyBlockIndices ) { System.Console.Write("{0} ", dependency); } System.Console.WriteLine(""); System.Console.WriteLine(""); blockCounter++; } System.Console.WriteLine("====="); } }
/*public static bool dumpResx(TextWriter wr, List<string> keys, List<string> values) { if (keys.Count <= 0) return false; for (int i = 0; i < keys.Count; i++) wr.AddResource(keys[i], values[i]); return true; }*/ /// <summary> /// Vytvoreni ResX z LMData. /// </summary> public static bool LMData2ResX(lm_scorm root, processStringsEvent onProcessString) { List<string> keys = new List<string>(); List<string> values = new List<string>(); TranslateInfo buf = new TranslateInfo(); foreach (TranslateInfo info in toTranslate(root, buf)) switch (info.Typ) { case LocalizeType.string2string: string valStr = (string)info.Value; if (string.IsNullOrEmpty(valStr) || !valStr.StartsWith(localizeStringPrefix)) continue; keys.Add(ResxFilesManager.resxId(info.Obj.varName, info.Field.Name.Substring(localizePrefix.Length))); //string.Format("Ui{0}_{1}", info.Obj.UniqueId, info.Field.Name.Substring(localizePrefix.Length))); values.Add(valStr.Substring(localizeStringPrefix.Length)); break; case LocalizeType.items2string: case LocalizeType.items2items: object[] objs = (object[])info.Value; foreach (LMScormObj item in objs) if (item is trans) { string txt; if (!tryTransText((trans)item, out txt)) continue; keys.Add(ResxFilesManager.resxId(item.varName, null)); values.Add(txt); } break; } return onProcessString(keys, values); //return createResx(fileName, keys, values); }
/// <summary> /// Pro jeden LMData objekt (soubor) vraci seznam properties k lokalizaci /// </summary> static IEnumerable<TranslateInfo> toTranslate(lm_scorm root, TranslateInfo buf) { foreach (LMScormObj obj in LMScormObj.GetAll(root)) { foreach (FieldInfo fi in obj.GetType().GetFields()) { object[] attrs = fi.GetCustomAttributes(typeof(LMScormLibDOM.LocalizedProperty), true); if (attrs == null || attrs.Length == 0) continue; LocalizedProperty attr = (LocalizedProperty)attrs[0]; if (!fi.Name.StartsWith(localizePrefix)) throw new Exception(); string nm = fi.Name.Substring(localizePrefix.Length); PropertyInfo prop = obj.GetType().GetProperty(nm); if (prop == null) if (attr.canMiss) continue; else throw new Exception(); object val = prop.GetValue(obj, null); if (val == null) continue; buf.Fill(attr.Type, obj, fi, val); yield return buf; } } }