예제 #1
0
        public void TestInspectParameters()
        {
            var a2 = new ColaNode("a2");
            var b1 = new ColaNode("b1");
            var a1 = new ColaNode("a1", a2, b1);

            a1.Script = "a11 = a1.a2.a22 + 2; a11 = a11;";
            a2.Script = "a11 = a1.a2.a22 + 2; a1.a11 = a11;";
            b1.Script = "a1.a2.a22 = b12 * b14";

            a2.ParamValues.Add(new Parameter("a22"), 22);

            var cc = new CocaContext(a1);
            var findings = cc.InspectCompositionAndParameters();
            ValidateInspectParametersFindings(findings);

            try
            {
                cc.Eval();
            }
            catch (FatalFindingsException ffe)
            {
                ValidateInspectParametersFindings(ffe.Findings);
                throw;
            }
        }
예제 #2
0
        public void TestInspectComposition()
        {
            var a2 = new ColaNode("a2");
            var b1 = new ColaNode("b1");
            var a1 = new ColaNode("a1", a2, b1);

            a1.Script = "a11 = a1.a2.a22 + 2; a11 *= a11;";
            a2.Script = "a11 = a1.a2.a22 + 2; a1.b1.b12 = a11";
            b1.Script = "a1.a2.a22 = b12 * b14";

            var cc = new CocaContext(a1);
            var findings = cc.InspectComposition();
            Assert.IsTrue(findings.AreFatal);

            Assert.AreEqual(2, findings.Count);
            Assert.IsTrue(findings[0] is ScriptIsErroneousFactum);
            Assert.IsTrue(findings[1] is DependencyGraphHasLoopFactum);

            var esf = findings[0] as ScriptIsErroneousFactum;
            Assert.AreEqual("[a1.a2, a1.b1, a1]", esf.Bottle.ToString());
            Assert.AreEqual("a1", esf.Node.ToString());
            var e = esf.Exception;
            var lightCode = e.SourceCode.RecalculateSourceCode();
            var lightSpan = e.ErrorSpan.RecalculateErrorSpan(e.SourceCode);
            Assert.AreEqual(ElfExceptionType.SyntaxError, e.Type);
            Assert.AreEqual("a11 = a1.a2.a22 + 2; a11 *= a11;", lightCode);
            Assert.AreEqual(Span.FromLength(26, 1), lightSpan);
            Assert.AreEqual("=", lightCode.Substring(lightSpan));
            Assert.AreEqual(null, e.AntlrNode);
            Assert.AreEqual(null, e.ElfNode);

            var dghlf = findings[1] as DependencyGraphHasLoopFactum;
            Assert.AreEqual("[a1.a2, a1.b1, a1]", esf.Bottle.ToString());
            AssertHelper.SequenceIsomorphic(new[] { a2, b1 }, dghlf.Loop);
        }
예제 #3
0
        public void SetUp()
        {
            var a2 = new ColaNode("a2");
            var b1 = new ColaNode("b1");
            var a1 = new ColaNode("a1", a2, b1);
            _cb = new ColaBottle(a1);

            a1.ParamValues.Add(new Parameter("a11"), 11);
            a2.ParamValues.Add(new Parameter("a22"), 22);
            b1.ParamValues.Add(new Parameter("b12"), 12);
            b1.ParamValues.Add(new Parameter("b14"), 14);

            a1.Script = "a11 = a1.a2.a22 + 2; a11 = a11 - a1.b1.b12;";
            a2.Script = "a11 = a1.a2.a22 + 2 / a1.b1.b14";
            b1.Script = "b12 = 1; b14 = 2; a1.a2.a22 = a1.a2.a22 * b12 * b14";
        }
예제 #4
0
파일: ColaBottle.cs 프로젝트: xeno-by/elf4b
 public ColaBottle(ColaNode cap)
 {
     Cap = cap;
 }
예제 #5
0
 public CocaContext(ColaNode cap) 
     : base(cap) 
 {
     VM = new VirtualMachine();
 }
예제 #6
0
        private ChangeSet Eval(ColaNode node)
        {
            var canonical = node.Script.ToCanonicalCola();
            var className = canonical.Substring(0, canonical.NthIndexOf(" ", 2)).Substring(4);

            // todo. regularly purge no more useful classes
            VM.Load(canonical);

            VM.Context["cc_values"] = _tempValues;
            VM.Context["cc_node"] = node;

            var changeset = new ChangeSet(() => _tempValues, v => { _tempValues = v; }).StartRecording();

            try
            {
                VM.CreateEntryPoint(className, "Main").RunTillEnd();
            }
            catch(ErroneousScriptException e)
            {
                // todo. a good idea would be to continue evaluation of everything that doesn't depend on this node
                var findings = InspectCompositionAndParameters();
                throw new FatalFindingsException(new Findings(
                    findings.Concat(new ScriptIsErroneousFactum(node, e).AsArray())));
            }

            return changeset.Capture();
        }
예제 #7
0
        public void TestEvalSuccess()
        {
            // construction test coca
            var a2 = new ColaNode("a2");
            var b1 = new ColaNode("b1");
            var a1 = new ColaNode("a1", a2, b1);
            a1.Script = "a11 = a1.a2.a22 + 2; a11 = a11;";
            a2.Script = "a11 = a1.a2.a22 + 2; a1.a11 = a11;";
            b1.Script = "a1.a2.a22 = b12 * b14";
            a2.ParamValues.Add(new Parameter("a22"), 22);
            b1.ParamValues.Add(new Parameter("b12"), 12);
            b1.ParamValues.Add(new Parameter("b14"), 14);
            var cc = new CocaContext(a1);

            // checking out the findings
            var findings = cc.InspectCompositionAndParameters();
            Assert.IsFalse(findings.AreFatal);
            Assert.AreEqual(2, findings.Count);
            Assert.IsTrue(findings[0] is ParameterValueIsNeverUsedFactum);
            Assert.IsTrue(findings[1] is ParameterIsMutatedSeveralTimesFactum);
            var pvinuf = findings[0] as ParameterValueIsNeverUsedFactum;
            Assert.AreEqual("[a1.a2, a1.b1, a1]", pvinuf.Bottle.ToString());
            Assert.AreEqual("a1.a2.a22", pvinuf.Param.ToString());
            var pimstf = findings[1] as ParameterIsMutatedSeveralTimesFactum;
            Assert.AreEqual("[a1.a2, a1.b1, a1]", pimstf.Bottle.ToString());
            Assert.AreEqual("a1.a11", pimstf.Param.ToString());
            AssertHelper.SequenceIsomorphic(new[] { "a1", "a1.a2" }, pimstf.Nodes.Select(n => n.ToString()));

            // ensuring that values and execution plan are fine
            Assert.AreEqual("[a1.a2.a22 = 22, a1.b1.b12 = 12, a1.b1.b14 = 14]", cc.ParamValues.ToString());
            Assert.AreEqual("a1.b1, a1, a1.a2", cc.ExecutionPlan.StringJoin());

            // run the calculations and check their results
            var cs = cc.Eval();
            Assert.AreEqual("[* = [a1.a2.a22 = 168], + = [a1.a11 = 170, a1.a2.a11 = 170], - = <empty>, " +
                "base = [a1.a2.a22 = 22, a1.b1.b12 = 12, a1.b1.b14 = 14]]", cs.ToString());

            // ensure that only after being accepted the results get saved to the coca
            Assert.AreEqual("[a1.a2.a22 = 22, a1.b1.b12 = 12, a1.b1.b14 = 14]", cc.ParamValues.ToString());
            cs.Accept();
            Assert.AreEqual("[a1.a2.a22 = 168, a1.a2.a11 = 170, " +
                "a1.b1.b12 = 12, a1.b1.b14 = 14, a1.a11 = 170]", cc.ParamValues.ToString());
        }