]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.mapping/src/org/simantics/mapping/rule/instructions/AndRuleInstruction.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.mapping / src / org / simantics / mapping / rule / instructions / AndRuleInstruction.java
diff --git a/bundles/org.simantics.mapping/src/org/simantics/mapping/rule/instructions/AndRuleInstruction.java b/bundles/org.simantics.mapping/src/org/simantics/mapping/rule/instructions/AndRuleInstruction.java
new file mode 100644 (file)
index 0000000..4f61ad4
--- /dev/null
@@ -0,0 +1,91 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.mapping.rule.instructions;\r
+\r
+import gnu.trove.map.hash.TIntIntHashMap;\r
+import gnu.trove.set.hash.TIntHashSet;\r
+\r
+import java.util.Arrays;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.utils.triggers.IModification;\r
+\r
+public class AndRuleInstruction implements IRuleInstruction {\r
+       IRuleInstruction[] rules;\r
+\r
+       public AndRuleInstruction(IRuleInstruction ... rules) {\r
+               this.rules = rules;\r
+       }\r
+\r
+       @Override\r
+       public IModification execute(ReadGraph g, Object[] bindings) throws DatabaseException {\r
+               for(int i=0;i<rules.length;++i) {\r
+                       if(DEBUG)\r
+                           System.out.println("AndRuleInstruction.execute " + i + "/" + rules.length);\r
+                       IRuleInstruction rule = rules[i];\r
+                       final IModification modi = rule.execute(g, bindings);\r
+                       if(modi != null) {\r
+                               final int si = i+1;\r
+                               if(si==rules.length)\r
+                                       return modi;\r
+                               else {\r
+                                       final Object[] curBindings = Arrays.copyOf(bindings, bindings.length);                                  \r
+                                       return new IModification() {\r
+\r
+                                               @Override\r
+                                               public void perform(WriteGraph g) throws DatabaseException {\r
+                                                       modi.perform(g);\r
+                                                       for(int j=si;j<rules.length;++j)\r
+                                                               rules[j].doExecute(g, curBindings);\r
+                                               }\r
+                                               \r
+                                               @Override\r
+                                               public String toString() {\r
+                                                   StringBuilder b = new StringBuilder();\r
+                                                   b.append("AndRuleInstruction");\r
+                                                   for(IRuleInstruction i : rules) i.toString(b, 2);\r
+                                                   return b.toString();\r
+                                               }\r
+                                               \r
+                                       };\r
+                               }\r
+                       }\r
+               }\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public void doExecute(WriteGraph g, Object[] bindings) throws DatabaseException {\r
+               for(IRuleInstruction rule : rules)\r
+                       rule.doExecute(g, bindings);\r
+       }\r
+       \r
+       @Override\r
+       public void collectVariables(TIntHashSet reads, TIntHashSet writes) {\r
+               for(IRuleInstruction r : rules)\r
+                       r.collectVariables(reads, writes);\r
+       }\r
+\r
+       @Override\r
+       public void mapVariables(TIntIntHashMap map) {\r
+               for(IRuleInstruction r : rules)\r
+                       r.mapVariables(map);\r
+       }\r
+       \r
+       @Override\r
+       public void toString(StringBuilder b, int indent) {\r
+               for(IRuleInstruction rule : rules)\r
+                       rule.toString(b, indent);\r
+       }\r
+}\r