/*******************************************************************************
 * Copyright (c) 2011 Oak Ridge National Laboratory.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package org.csstudio.alarm.beast;

import java.io.PrintStream;

/** Create test IOC database and alarm config file
 *  @author Kay Kasemir
 */
@SuppressWarnings("nls")
public class CreateTestDB
{
	// 5000 PVs in groups of 100 operate like this:
	//
	// PVs 000..099 Ramp 0..5000 and alarm at  90..100
	// PVs 100..199 Ramp 0..5000 and alarm at 190..200
	//
	// When not latching, they alarm from 0-100, 100-200, ...
	// to get a longer lasting alarm which then auto-resets
	final private static int COUNT = 5000;
	final private static int GROUPSIZE = 100;
	final private static boolean latch = false;
	
    public static void main(String[] args) throws Exception
    {
		PrintStream out = new PrintStream("demo.db");
		
		for (int i=0; i<COUNT; ++i)
		{
			final String num = String.format("%04d", i);
			
			final int group = (1 + i/GROUPSIZE) * GROUPSIZE;
			
			out.println("# Group " + group);
			out.println("record(calc, \"Ramp" + num + "\")");
			out.println("{");
			out.println("    field(INPA, \"Ramp" + num + "\")");
			out.println("    field(CALC, \"A<" + COUNT + "?A+1:0\")");
			out.println("    field(SCAN, \"1 second\")");
			out.println("    field(HOPR, \"" + COUNT + "\")");
			out.println("    field(FLNK, \"Alarm" + num + "\")");
			out.println("}");
			out.println("record(calc, \"Alarm" + num + "\")");
			out.println("{");
			out.println("    field(INPA, \"Ramp" + num + "\")");
			final int low = latch
				? group - GROUPSIZE/10
				: group - GROUPSIZE;
			out.println("    field(CALC, \"A>=" + low + "&&A<=" + group + "\")");
			out.println("    field(HIGH, \"1\")");
			out.println("    field(HSV , \"MINOR\")");
			out.println("}");
			out.println();
		}
		out.close();
		
		out = new PrintStream("demo.xml");
		out.println("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
		out.println("<config name=\"demo\">");
		out.println("<component name=\"Area0000\">");
		for (int i=0; i<COUNT; ++i)
		{
			final String num = String.format("%04d", i);
			if ((i % GROUPSIZE) == 0)
			{
		        out.println("</component>");
				out.println("<component name=\"Area" + num + "\">");
				
			}
			out.println("    <pv name=\"Alarm" + num + "\">");
			out.println("        <description>Test PV</description>");
			out.println("        <latching>" + latch + "</latching>");
			out.println("        <annunciating>true</annunciating>");
			out.println("        <guidance>");
			out.println("            <title>Test</title>");
			out.println("            <details>You can&#039;t do anything, this is just a test</details>");
			out.println("        </guidance>");
			out.println("    </pv>");
		}
        out.println("</component>");
        out.println("</config>");
		out.close();
    }
}

@@ -679,16 +679,17 @@
 %09// 5000
+0
  PVs in 
@@ -738,18 +738,20 @@
  PVs 000
+0
 ..
+0
 099 Ramp
@@ -794,13 +794,15 @@
 PVs 
+0
 100..
+0
 199 
@@ -998,16 +998,17 @@
 T = 5000
+0
 ;%0D%0A%09fina
@@ -1040,17 +1040,17 @@
 PSIZE = 
-1
+2
 00;%0D%0A%09fi
@@ -1283,33 +1283,33 @@
 tring.format(%22%250
-4
+5
 d%22, i);%0D%0A%09%09%09%0D%0A%09%09
@@ -2391,57 +2391,8 @@
 );%0D%0A
-%09%09out.println(%22%3Ccomponent name=%5C%22Area0000%5C%22%3E%22);%0D%0A
 %09%09fo
@@ -2466,9 +2466,9 @@
 (%22%250
-4
+5
 d%22, 
@@ -2506,54 +2506,8 @@
 0)%0D%0A
-%09%09%09%7B%0D%0A%09%09        out.println(%22%3C/component%3E%22);%0D%0A
 %09%09%09%09
@@ -2564,20 +2564,8 @@
 );%0D%0A
-%09%09%09%09%0D%0A%09%09%09%7D%0D%0A
 %09%09%09o

Test setup...