From eae0f2550ab42da0710bfd0ac13f932a6491a243 Mon Sep 17 00:00:00 2001 From: Omarnael <47508294+Omarnael@users.noreply.github.com> Date: Wed, 15 Jul 2020 17:05:18 +0200 Subject: [PATCH 1/4] Create NTest.java New Network Tests --- tests/tests/NTest.java | 1772 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1772 insertions(+) create mode 100644 tests/tests/NTest.java diff --git a/tests/tests/NTest.java b/tests/tests/NTest.java new file mode 100644 index 00000000..7d28ee25 --- /dev/null +++ b/tests/tests/NTest.java @@ -0,0 +1,1772 @@ +package tests; + +import sneps.exceptions.CannotBuildNodeException; +import sneps.exceptions.CaseFrameCannotBeRemovedException; +import sneps.exceptions.CaseFrameMissMatchException; +import sneps.exceptions.ContextNameDoesntExistException; +import sneps.exceptions.ContradictionFoundException; +import sneps.exceptions.DuplicatePropositionException; +import sneps.exceptions.EquivalentNodeException; +import sneps.exceptions.IllegalIdentifierException; +import sneps.exceptions.NodeCannotBeRemovedException; +import sneps.exceptions.NodeNotFoundInNetworkException; +import sneps.exceptions.NodeNotFoundInPropSetException; +import sneps.exceptions.NotAPropositionNodeException; +import sneps.exceptions.RelationDoesntExistException; +import sneps.exceptions.SemanticNotFoundInNetworkException; +import sneps.network.Network; +import sneps.network.Node; +import sneps.network.PropositionNode; +import sneps.network.VariableNode; +import sneps.network.cables.DownCable; +import sneps.network.cables.DownCableSet; +import sneps.network.cables.UpCable; +import sneps.network.cables.UpCableSet; +import sneps.network.classes.CFSignature; +import sneps.network.classes.CableTypeConstraint; +import sneps.network.classes.CaseFrame; +import sneps.network.classes.PathTrace; +import sneps.network.classes.RCFP; +import sneps.network.classes.Relation; +import sneps.network.classes.RelationsRestrictedCaseFrame; +import sneps.network.classes.Semantic; +import sneps.network.classes.SemanticHierarchy; +import sneps.network.classes.SubDomainConstraint; +import sneps.network.classes.Wire; +import sneps.network.classes.setClasses.NodeSet; +import sneps.network.classes.setClasses.PropositionSet; +import sneps.network.classes.setClasses.VariableSet; +import sneps.network.classes.term.Base; +import sneps.network.classes.term.Closed; +import sneps.network.classes.term.Molecular; +import sneps.network.classes.term.Open; +import sneps.network.classes.term.Term; +import sneps.network.classes.term.Variable; +import sneps.network.paths.AndPath; +import sneps.network.paths.BUnitPath; +import sneps.network.paths.BangPath; +import sneps.network.paths.CFResBUnitPath; +import sneps.network.paths.CFResFUnitPath; +import sneps.network.paths.ComposePath; +import sneps.network.paths.ConversePath; +import sneps.network.paths.DomainRestrictPath; +import sneps.network.paths.FUnitPath; +import sneps.network.paths.IrreflexiveRestrictPath; +import sneps.network.paths.KPlusPath; +import sneps.network.paths.KStarPath; +import sneps.network.paths.OrPath; +import sneps.network.paths.Path; +import sneps.network.paths.RangeRestrictPath; +import sneps.snebr.Controller; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.Hashtable; +import java.util.LinkedList; +import java.util.Map.Entry; +import java.util.Set; + +import org.junit.*; + +public class NTest { + static Semantic semantic; + final static String semanticType = "Proposition"; + private static final Semantic Semantic = null; + + @Before + public void setUp() throws SemanticNotFoundInNetworkException { + Network.clearNetwork(); + Controller.clearSNeBR(); + semantic = new Semantic(semanticType); + sneps.network.classes.Semantic.createDefaultSemantics(); + Network.defineDefaults(); + + } + + + + //NEW TEST + @Test + public void testGreatestLowerBound() throws SemanticNotFoundInNetworkException { + + + SemanticHierarchy.createSemanticType("Animal","Individual"); + SemanticHierarchy.createSemanticType("Companion","Individual"); + SemanticHierarchy.createSemanticType("Test","Animal"); + + ArrayList sems = new ArrayList(); + sems.add("Animal"); + sems.add("Companion"); + + SemanticHierarchy.createSemanticType("Pet", sems); + SemanticHierarchy.createSemanticType("Target","Pet"); + + + Semantic sem1 = SemanticHierarchy.getSemantic("Animal"); + Semantic sem2 = SemanticHierarchy.getSemantic("Companion"); + + Semantic target = SemanticHierarchy.getSemantic("Target"); + Semantic glb = SemanticHierarchy.greatestLowerBound(sem1,sem2); + + assertEquals(glb,target); + + } + + + //NEW TEST + @SuppressWarnings("static-access") + @Test + public void testCompact() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, NodeCannotBeRemovedException, NodeNotFoundInPropSetException { + + Network.clearNetwork(); + Network.buildBaseNode("x0", semantic.individual); + Network.buildBaseNode("x1", semantic.individual); + Network.buildBaseNode("x2", semantic.individual); + Network.buildBaseNode("x3", semantic.individual); + Network.buildBaseNode("x4", semantic.individual); + + + System.out.println("-------------------------------------------------------------" + Network.getNode("x0")); + + assertEquals(Network.getNodesWithIDs().size(),5); + assertEquals(Network.getNodesWithIDs().get(2).toString(),"x2"); + + + Network.removeNode(Network.getNode("x2")); + + assertEquals(Network.getNodesWithIDs().size(),5); + assertEquals(Network.getNodesWithIDs().get(2),null); + + + Base b = new Base("x2"); + Node x2 = new Node(b); + + Network.AddCompact(x2); + + + assertEquals(Network.getNodesWithIDs().size(),5); + assertEquals(Network.getNodesWithIDs().get(2).toString(),"x2"); + + + + + } + + + + + + + //NEW TEST + @Test + public void testNetworkRelationMethods() throws RelationDoesntExistException, CaseFrameCannotBeRemovedException { + + //create relation + String relationA = "A"; + Relation A = Network.defineRelation(relationA, semanticType); + + //System.out.println(Network.getRelation(relationName1)); + + //get relation using string + assertEquals(A ,Network.getRelation(relationA)); + + //try getting relation that does not exists + String name = "test"; + try { + Network.getRelation(name); + } + + catch(Exception e) { + assertEquals("There is no relation with the following name: " + name, e.getMessage()); + } + + //delete relation + Network.undefineRelation(relationA); + + + try { + Network.getRelation(relationA); + } + + catch(Exception e) { + assertEquals("There is no relation with the following name: " + relationA, e.getMessage()); + } + + } + + + + + + + + + @Test + public void testSemanticCreation() throws SemanticNotFoundInNetworkException { + + // A should have no super class other than Entity + SemanticHierarchy.createSemanticType("A"); + + Semantic A = SemanticHierarchy.getSemantic("A"); + + LinkedList superClassesOfA = A.getSuperClassesNames(); + + assertEquals(superClassesOfA.size(), 1); + assertTrue(superClassesOfA.contains("Entity")); + + // try creating a semantic class with a super that does not exist + try { + SemanticHierarchy.createSemanticType("B", "C"); + } + catch(Exception e) { + assertEquals("The super class named 'C' does not exist!", e.getMessage()); + } + + // test creation of a semantic class with a super class + Semantic B = SemanticHierarchy.createSemanticType("B", "A"); + + LinkedList superClassesOfB = B.getSuperClassesNames(); + + assertTrue(superClassesOfB.contains("A")); + assertNotNull(B); + + + } + + + @Test + public void TestSuperSemantic() throws SemanticNotFoundInNetworkException { + // A has no super class + Semantic A = SemanticHierarchy.createSemanticType("A"); + + // A is the super class of B + Semantic B = SemanticHierarchy.createSemanticType("B", "A"); + + // B is the super class of C and D + Semantic C = SemanticHierarchy.createSemanticType("C", "B"); + Semantic D = SemanticHierarchy.createSemanticType("D", "C"); + + LinkedList superClassesOfD = D.getSuperClassesNames(); + LinkedList superClassesOfC = C.getSuperClassesNames(); + LinkedList superClassesOfB = B.getSuperClassesNames(); + LinkedList superClassesOfA = A.getSuperClassesNames(); + + // check on the super classes of D + assertEquals(superClassesOfD.size(), 4); + assertTrue(superClassesOfD.contains("A")); + assertTrue(superClassesOfD.contains("B")); + assertTrue(superClassesOfD.contains("C")); + assertTrue(superClassesOfD.contains("Entity")); + + // check on the super classes of C + assertEquals(superClassesOfC.size(), 3); + assertTrue(superClassesOfC.contains("A")); + assertTrue(superClassesOfC.contains("B")); + assertTrue(superClassesOfD.contains("Entity")); + + + // check on the super classes of B + assertEquals(superClassesOfB.size(), 2); + assertTrue(superClassesOfB.contains("A")); + assertTrue(superClassesOfB.contains("Entity")); + + // check on the super classes of A + assertEquals(superClassesOfA.size(), 1); + assertTrue(superClassesOfA.contains("Entity")); + + + } + + @Test + public void TestRelationsWithAdjustAndLimit() throws RelationDoesntExistException, CaseFrameCannotBeRemovedException { + Network.clearNetwork(); + String relationName = "relation name"; + int limit = 3; + String adjust = "expand"; + Network.defineRelation(relationName, semanticType, adjust, limit); + + // test creation of a new relation + Relation relation = Network.getRelation(relationName); + assertNotNull(relation); + assertEquals(relation.getName(), relationName); + assertEquals(relation.getType(), semanticType); + assertEquals(relation.getAdjust(), adjust); + assertEquals(relation.getLimit(), limit); + + // test that the code does not create two relations with the same name + // and returns the already existing relation instead + Relation newRelation = Network.defineRelation(relationName, semanticType); + assertEquals(relation, newRelation); + assertEquals(1, Network.getRelations().keySet().size()); + + + } + + + @Test + public void testRelationCreationWithoutAdjustAndLimitValues() throws RelationDoesntExistException { + + Network.clearNetwork(); + String relationName2 = "relation name 2"; + Network.defineRelation(relationName2, semanticType); + + // test creation of a new relation + Relation relation = Network.getRelation(relationName2); + assertNotNull(relation); + assertEquals(relation.getName(), relationName2); + assertEquals(relation.getType(), semanticType); + assertEquals(relation.getAdjust(), "none"); + assertEquals(relation.getLimit(), 1); + + // test that the code does not create two relations with the same name + // and returns the already existing relation instead + Relation newRelation = Network.defineRelation(relationName2, semanticType); + assertEquals(relation, newRelation); + assertEquals(1, Network.getRelations().keySet().size()); + } + + + + + @Test + public void testCaseFrameCreation() { + Network.clearNetwork(); + LinkedList relations = new LinkedList(); + relations.add(Network.defineRelation("relation 1", semanticType)); + relations.add(Network.defineRelation("relation 2", semanticType)); + relations.add(Network.defineRelation("relation 3", semanticType)); + + + CaseFrame caseFrame = Network.defineCaseFrame(semanticType, relations); + + // make sure that it does not create duplicate case frames + Network.defineCaseFrame(semanticType, relations); + assertEquals(1, Network.getCaseFrames().keySet().size()); + + + assertEquals(caseFrame.getRelations().size(), 3); + assertEquals(caseFrame.getSemanticClass(), semanticType); + } + + + @Test + public void testRelationsRestrictedCaseFrameCreation() { + + Network.clearNetwork(); + LinkedList RCFPs = new LinkedList(); + RCFPs.add(new RCFP(Network.defineRelation("relation1", semanticType, "none", 1), "reduce", 5)); + RCFPs.add(new RCFP(Network.defineRelation("relation2", semanticType, "none", 1), "expand", 3)); + + RelationsRestrictedCaseFrame RRCF = Network.defineCaseFrameWithConstraints(semanticType, RCFPs); + + // make sure that it does not create duplicate case frames + Network.defineCaseFrameWithConstraints(semanticType, RCFPs); + assertEquals(1, Network.getCaseFrames().keySet().size()); + + assertEquals(RRCF.getRelations().size(), 2); + assertEquals(RRCF.getSemanticClass(), semanticType); + + + } + + + + //NEW TEST + @Test + public void testCFConflicts() { + + Network.clearNetwork(); + LinkedList RCFPs = new LinkedList(); + RCFPs.add(new RCFP(Network.defineRelation("relation1", semanticType, "none", 1), "reduce", 5)); + RCFPs.add(new RCFP(Network.defineRelation("relation2", semanticType, "none", 1), "expand", 3)); + RCFPs.add(new RCFP(Network.defineRelation("relation5", semanticType, "none", 0), "expand", 1)); + + RelationsRestrictedCaseFrame RRCF = Network.defineCaseFrameWithConstraints(semanticType, RCFPs); + System.out.println(Network.getCaseFrames()); + + LinkedList RCFPs1 = new LinkedList(); + RCFPs1.add(new RCFP(Network.defineRelation("relation1", semanticType, "none", 1), "reduce", 5)); + RCFPs1.add(new RCFP(Network.defineRelation("relation2", semanticType, "none", 1), "expand", 3)); + RCFPs1.add(new RCFP(Network.defineRelation("relation3", semanticType, "none", 0), "reduce", 1)); + RCFPs1.add(new RCFP(Network.defineRelation("relation4", semanticType, "none", 0), "expand", 1)); + + RelationsRestrictedCaseFrame RRCF1 = Network.defineCaseFrameWithConstraints(semanticType, RCFPs1); + + System.out.println(Network.getCaseFrames()); + } + + + + + //NEW TEST + @Test + public void testCFSignatue() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, CannotBuildNodeException, EquivalentNodeException, CaseFrameMissMatchException, SemanticNotFoundInNetworkException { + + Network.clearNetwork(); + LinkedList rules = new LinkedList(); + LinkedList checks = new LinkedList(); + ArrayList wires = new ArrayList(); + LinkedList relations = new LinkedList<>(); + + SubDomainConstraint sd = new SubDomainConstraint("relation 1",checks); + SubDomainConstraint sd1 = new SubDomainConstraint("relation 2",checks); + CableTypeConstraint c = new CableTypeConstraint(semanticType,1,10); + CableTypeConstraint c1 = new CableTypeConstraint(semanticType,1,9); + checks.add(c); + checks.add(c1); + rules.add(sd); + rules.add(sd1); + + CFSignature s = new CFSignature("A",rules,"1"); + assertNotNull(s.getId()); + + // populate the relation and wire lists + Relation relation1 = Network.defineRelation("relation 1", semanticType,"none",1); + Relation relation2 = Network.defineRelation("relation 2", semanticType,"none",1); + relations.add(relation1); + relations.add(relation2); + + wires.add(new Wire(relation1, Network.buildBaseNode("b1", semantic))); + wires.add(new Wire(relation2, Network.buildBaseNode("b2", semantic))); + + + + LinkedList RCFPs = new LinkedList(); + RCFPs.add(new RCFP(relation1, "reduce", 1)); + RCFPs.add(new RCFP(relation2, "expand", 1)); + RelationsRestrictedCaseFrame RRCF = Network.defineCaseFrameWithConstraints(semanticType, RCFPs); + Network.addSignatureToCaseFrame(s, 0 , RRCF); + + Object[][] array = Network.turnWiresIntoArray(wires); + Object[][] relNodeSet = Network.turnIntoRelNodeSet(array); + + + Network.buildMolecularNode(wires, RRCF); + NodeSet ns = Network.getNodesHavingCF(RRCF); + assertNotNull(ns); + assertEquals(Network.getNodesHavingCF(RRCF).toString(),ns.toString()); + + assertEquals(semanticType,Network.getCFSignature(Network.turnIntoHashtable(relNodeSet), RRCF)); + + + + } + + + //NEW TEST + @Test + public void testUndefineCaseFrame() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, CannotBuildNodeException, EquivalentNodeException, CaseFrameMissMatchException, SemanticNotFoundInNetworkException, CaseFrameCannotBeRemovedException, NodeCannotBeRemovedException, NodeNotFoundInPropSetException, RelationDoesntExistException { + Network.clearNetwork(); + ArrayList wires = new ArrayList(); + LinkedList relations = new LinkedList<>(); + + // populate the relation and wire lists + Relation relation1 = Network.defineRelation("relation 1", semanticType); + Relation relation2 = Network.defineRelation("relation 2", semanticType); + Relation relation3 = Network.defineRelation("relation 3", semanticType); + + relations.add(relation1); + relations.add(relation2); + relations.add(relation3); + + wires.add(new Wire(relation1, Network.buildBaseNode("b1", semantic))); + wires.add(new Wire(relation2, Network.buildBaseNode("b2", semantic))); + wires.add(new Wire(relation3, Network.buildBaseNode("b3", semantic))); + + // define a case frame for the node + CaseFrame caseFrame = Network.defineCaseFrame(semanticType, relations); + + + Node n = Network.buildMolecularNode(wires, caseFrame); + + //TRY TO REMOVE CASE FRAME WITH IMPLEMENTED NODES + try { + Network.undefineCaseFrame(caseFrame.getId()); + } + catch(Exception e) { + assertEquals("Case frame can not be removed .. " + "remove the nodes implementing this case frame first", e.getMessage()); + } + + //REMOVE NODES FIRST + Network.removeNode(Network.getNode("M1")); + try { + Network.undefineCaseFrame(caseFrame.getId()); + } + catch(Exception e) { + assertEquals("Case frame can not be removed .. " + "remove the nodes implementing this case frame first", e.getMessage()); + } + + assertTrue(Network.getNodes().isEmpty()); + assertTrue(Network.getCaseFrames().isEmpty()); + + } + + + + //NEW TEST + @Test + public void testSubDomainConstraint() { + + LinkedList checks = new LinkedList(); + LinkedList rules = new LinkedList(); + SubDomainConstraint sd = new SubDomainConstraint("relation 1",checks); + CableTypeConstraint c = new CableTypeConstraint(semanticType,1,10); + CableTypeConstraint c1 = new CableTypeConstraint(semanticType,1,9); + checks.add(c); + checks.add(c1); + rules.add(sd); + + assertEquals(1,rules.size()); + } + + + //NEW TEST + @Test + public void testCableTypeConstraint() { + CableTypeConstraint c = new CableTypeConstraint("A",1,2); + assertEquals(c.getSemanticType(),"A"); + assertEquals(c.getId(),"A,1,2"); + } + + + @Test + public void testDownCableSetCreation() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException { + // create the relations and add them to a linked list. + // this linked list will be used to create the case frame + Relation relation1 = new Relation("relation1", semanticType); + Relation relation2 = new Relation("relation2", semanticType); + LinkedList relationList = new LinkedList(); + relationList.add(relation1); + relationList.add(relation2); + + // build two base nodes and put each one in a different node set + // each node set will be used to create a down cable + + + Node node1 = Network.buildBaseNode("n1", semantic); + Node node2 = Network.buildBaseNode("n2", semantic); + NodeSet nodeSet1 = new NodeSet(); + NodeSet nodeSet2 = new NodeSet(); + nodeSet1.addNode(node1); + nodeSet2.addNode(node2); + + // create the case frame and down cables + CaseFrame caseFrame = Network.defineCaseFrame(semanticType, relationList); + + DownCable cable1 = new DownCable(relation1, nodeSet1); + DownCable cable2 = new DownCable(relation2, nodeSet2); + LinkedList cableList = new LinkedList(); + cableList.add(cable1); + cableList.add(cable2); + + // create the down cable set + DownCableSet cableSet = new DownCableSet(cableList, caseFrame); + + cable1 = cableSet.getDownCable("relation1"); + cable2 = cableSet.getDownCable("relation2"); + + assertFalse((cable1.getNodeSet().equals(cable2.getNodeSet()))); + //assertTrue(nodeSet2.contains(node2)); + + assertFalse(cable1.equals(cable2)); + + + + } + + + + + //NEW TEST + @Test + public void testUpCableSet() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, CannotBuildNodeException, EquivalentNodeException, CaseFrameMissMatchException, SemanticNotFoundInNetworkException { + + Network.clearNetwork(); + ArrayList wires = new ArrayList(); + LinkedList relations = new LinkedList<>(); + + // populate the relation and wire lists + Relation relation1 = Network.defineRelation("relation 1", semanticType); + Relation relation2 = Network.defineRelation("relation 2", semanticType); + Relation relation3 = Network.defineRelation("relation 3", semanticType); + + relations.add(relation1); + relations.add(relation2); + relations.add(relation3); + + wires.add(new Wire(relation1, Network.buildBaseNode("b1", semantic))); + wires.add(new Wire(relation2, Network.buildBaseNode("b2", semantic))); + wires.add(new Wire(relation3, Network.buildBaseNode("b3", semantic))); + + // define a case frame for the node + CaseFrame caseFrame = Network.defineCaseFrame(semanticType, relations); + + // create the node and check it was created successfully + Network.buildMolecularNode(wires, caseFrame); + + //System.out.println(Network.getNode("b1").getUpCableSet().getUpCables().get("relation 1").toString()); + String s = Network.getNode("b3").getUpCableSet().getUpCables().get("relation 3").toString(); + String NameOfRelation = s.substring(0,10); + assertEquals(relation3.toString(),NameOfRelation); + + } + + + + //NEW TEST + @Test + public void testBuildBaseNode() throws IllegalIdentifierException, NotAPropositionNodeException, NodeNotFoundInNetworkException, NodeCannotBeRemovedException, NodeNotFoundInPropSetException { + Network.clearNetwork(); + //BUILD NEW BASE NODE + int sizeOfNodes = Network.getNodes().size(); + int sizeOfProps = Network.getPropositionNodes().size(); + + Network.buildBaseNode("n0", semantic); + Node n0 = Network.getNode("n0"); + assertTrue(Network.getNodeById(n0.getId()) instanceof PropositionNode); + assertEquals(n0, Network.getNodeById(n0.getId())); + assertTrue(n0.getTerm() instanceof Base); + assertEquals(Network.getNodes().size(), sizeOfNodes + 1); + assertEquals(Network.getPropositionNodes().size(), sizeOfProps + 1); + + //System.out.println(Network.getNodes()); + + //CHECK NO DUPLICATE NODES + Network.buildBaseNode("n0", semantic); + assertEquals(Network.getNodes().size(), sizeOfNodes + 1); + assertEquals(Network.getPropositionNodes().size(), sizeOfProps + 1); + + + //CHECK NO ACT NODES + try { + Network.buildBaseNode("n0", semantic.act); + } + + catch (Exception e) { + assertEquals(Network.getNodes().size(), sizeOfNodes + 1); + assertEquals(Network.getPropositionNodes().size(), sizeOfProps + 1); + assertEquals("ERROR: Acts cannot be base nodes!!!",e.getMessage()); + + } + + //CHECK DUPLICATE VARIABLE NODE + Network.buildVariableNode("n1"); + try { + Network.buildBaseNode("n1", semantic); + + } + + catch (Exception e) { + assertEquals("A variable node already exists with this identifier.",e.getMessage()); + + } + + + + + } + + + //NEW TEST + @Test + public void testBuildVariableNode() throws IllegalIdentifierException { + Network.clearNetwork(); + + Node n = Network.buildVariableNode(); + assertEquals("V1",n.getIdentifier()); + Semantic sem = Semantic.entity; + Semantic sem2 = semantic; + assertEquals(sem,n.getSemantic()); + + + Node n1 = Network.buildVariableNode("n1"); + assertEquals("n1",n1.getIdentifier()); + assertEquals(sem,n1.getSemantic()); + + Node n2 = Network.buildVariableNode(sem2); + assertEquals(sem2,n2.getSemantic()); + + + } + + + + @Test + public void testBuildMolecularNode() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, CannotBuildNodeException, EquivalentNodeException, CaseFrameMissMatchException, SemanticNotFoundInNetworkException, RelationDoesntExistException { + Network.clearNetwork(); + ArrayList wires = new ArrayList(); + ArrayList wires1 = new ArrayList(); + LinkedList relations = new LinkedList<>(); + LinkedList relations1 = new LinkedList<>(); + + // populate the relation and wire lists + Relation relation1 = Network.defineRelation("relation 1", semanticType); + Relation relation2 = Network.defineRelation("relation 2", semanticType); + Relation relation3 = Network.defineRelation("relation 3", semanticType); + Relation relation4 = Network.defineRelation("relation 4", semanticType); + Relation relation5 = Network.defineRelation("relation 5", semanticType); + + relations.add(relation1); + relations.add(relation2); + relations.add(relation3); + + relations1.add(relation4); + relations1.add(relation5); + + + wires.add(new Wire(relation1, Network.buildBaseNode("b1", semantic))); + wires.add(new Wire(relation2, Network.buildBaseNode("b2", semantic))); + wires.add(new Wire(relation3, Network.buildVariableNode(semantic))); + + wires1.add(new Wire(relation4, Network.buildBaseNode("b1", semantic))); + wires1.add(new Wire(relation5, Network.buildBaseNode("b3", semantic))); + + // define a case frame for the node + CaseFrame caseFrame = Network.defineCaseFrame(semanticType, relations); + CaseFrame caseFrame1 = Network.defineCaseFrame(semanticType, relations1); + + // create the node and check it was created successfully + Node openNode = Network.buildMolecularNode(wires, caseFrame); + Node node = Network.buildMolecularNode(wires1, caseFrame1); + assertEquals(Network.getMolecularNodes().keySet().size(), 2); + assertTrue(openNode instanceof PropositionNode); + assertEquals(Network.getNodes().size(), 6); + assertEquals(Network.getPropositionNodes().size(), 5); + + Term term = openNode.getTerm(); + assertTrue(term instanceof Open); + + NodeSet ns = Network.getNodesHavingCF(caseFrame); + assertNotNull(ns); + //assertEquals("{P1:(relation 3 {V1} relation 2 {b2} relation 1 {b1})}",ns.toString()); + + assertEquals(1,Network.getAllVariables((Molecular) term).size()); + + //System.out.println(Network.getNode("b1").getUpCableSet().getUpCable("relation 1").getRelation().getType()); + //System.out.println(Network.getMolecularNodes().get(arg0)); + //System.out.println(Network.getNode("b1").getUpCableSet().getUpCables().keySet()); + + } + + + + + //NEW TEST + @Test + public void testMolNodeRRCF() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, CannotBuildNodeException, EquivalentNodeException, CaseFrameMissMatchException, SemanticNotFoundInNetworkException { + Network.clearNetwork(); + ArrayList wires = new ArrayList(); + LinkedList relations = new LinkedList<>(); + + // populate the relation and wire lists + Relation relation1 = Network.defineRelation("relation 1", semanticType,"none",1); + Relation relation2 = Network.defineRelation("relation 2", semanticType,"none",1); + + + relations.add(relation1); + relations.add(relation2); + + + wires.add(new Wire(relation1, Network.buildBaseNode("b1", semantic))); + wires.add(new Wire(relation2, Network.buildBaseNode("b2", semantic))); + + LinkedList RCFPs = new LinkedList(); + RCFPs.add(new RCFP(relation1, "reduce", 1)); + RCFPs.add(new RCFP(relation2, "expand", 1)); + + RelationsRestrictedCaseFrame RRCF = Network.defineCaseFrameWithConstraints(semanticType, RCFPs); + + Node closedNode = Network.buildMolecularNode(wires, RRCF); + assertEquals(Network.getMolecularNodes().keySet().size(), 1); + assertTrue(closedNode instanceof PropositionNode); + assertEquals(Network.getNodes().size(), 3); + assertEquals(Network.getPropositionNodes().size(), 3); + + Term term = closedNode.getTerm(); + assertTrue(term instanceof Closed); + + NodeSet ns = Network.getNodesHavingCF(RRCF); + assertNotNull(ns); + //assertEquals("{M1:(relation 2 {b2} relation 1 {b1})}",ns.toString()); + + } + + @Test + public void testBuildMolecularNodeNotFollowingCaseFrame() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, CannotBuildNodeException, EquivalentNodeException { + Network.clearNetwork(); + ArrayList wires = new ArrayList(); + LinkedList relations = new LinkedList<>(); + + // populate the relation and wire lists + Relation relation1 = Network.defineRelation("relation 1", semanticType); + Relation relation2 = Network.defineRelation("relation 2", semanticType); + Relation relation3 = Network.defineRelation("relation 3", semanticType); + + // only add two relations to the case frame relations + relations.add(relation1); + relations.add(relation2); + + wires.add(new Wire(relation1, Network.buildBaseNode("b1", semantic))); + wires.add(new Wire(relation2, Network.buildBaseNode("b2", semantic))); + wires.add(new Wire(relation3, Network.buildBaseNode("b3", semantic))); + + // define a case frame for the node + CaseFrame caseFrame = Network.defineCaseFrame(semanticType, relations); + + // create the node and check if it was created + Node closedNode = null; + try { + closedNode = Network.buildMolecularNode(wires, caseFrame); + } + catch(Exception e) { + assertNull(closedNode); + assertTrue(e instanceof CaseFrameMissMatchException); + assertEquals(e.getMessage(), "Not following the case frame .. wrong node set size or wrong set of relations"); + assertEquals(Network.getNodes().size(), 3); + String caseFrameID = caseFrame.getId(); + NodeSet molecularNodesBelongingToCaseFrame = Network.getMolecularNodes().get(caseFrameID); + assertEquals(molecularNodesBelongingToCaseFrame.size(), 0); + } + } + + + + @Test + public void testBuildMolecularNodeInvalidRelNodeSet() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, CannotBuildNodeException, EquivalentNodeException { + Network.clearNetwork(); + ArrayList wires = new ArrayList(); + LinkedList relations = new LinkedList<>(); + + // populate the relation and wire lists + Relation relation1 = Network.defineRelation("relation 1", semanticType); + Relation relation2 = Network.defineRelation("relation 2", semanticType); + Relation relation3 = Network.defineRelation("relation 3", semanticType); + + relations.add(relation1); + relations.add(relation2); + relations.add(relation3); + + wires.add(new Wire(relation1, Network.buildBaseNode("b1", semantic))); + wires.add(new Wire(relation3, Network.buildBaseNode("b2", semantic))); + + // make one of the relations point to a node that it is not allowed to point at + wires.add(new Wire(relation2, Network.buildBaseNode("b3", Semantic.individual))); + + // define a case frame for the node + CaseFrame caseFrame = Network.defineCaseFrame(semanticType, relations); + + // create the node and check if it was created + Node closedNode = null; + try { + closedNode = Network.buildMolecularNode(wires, caseFrame); + } + catch(Exception e) { + assertNull(closedNode); + assertTrue(e instanceof CannotBuildNodeException); + assertEquals(e.getMessage(), "Cannot build the node .. the relation node pairs are not valid"); + assertEquals(Network.getNodes().size(), 3); + String caseFrameID = caseFrame.getId(); + NodeSet molecularNodesBelongingToCaseFrame = Network.getMolecularNodes().get(caseFrameID); + assertEquals(molecularNodesBelongingToCaseFrame.size(), 0); + + } + } + + + + + + + //NEW TEST + @Test + public void testNodeSet() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException { + + NodeSet n = new NodeSet(); + Node node1 = Network.buildBaseNode("n1", Semantic.individual); + Node node2 = Network.buildBaseNode("n2", Semantic.individual); + Node node3 = Network.buildBaseNode("n3", semantic); + n.addNode(node1); + n.addNode(node2); + n.addNode(node3); + + NodeSet n1 = new NodeSet(); + Node node4 = Network.buildBaseNode("n4", Semantic.individual); + Node node5 = Network.buildBaseNode("n5", Semantic.individual); + n1.addNode(node4); + n1.addNode(node5); + n1.addNode(node3); + + + + + + NodeSet ExpectedUnion = new NodeSet(); + + ExpectedUnion.addNode(node1); + ExpectedUnion.addNode(node2); + ExpectedUnion.addNode(node3); + + ExpectedUnion.addNode(node4); + ExpectedUnion.addNode(node5); + ExpectedUnion.addNode(node3); + + + NodeSet ExpectedIntersection = new NodeSet(); + + ExpectedIntersection.addNode(node3); + + + NodeSet ExpectedDiff = new NodeSet(); + ExpectedDiff.addNode(node4); + ExpectedDiff.addNode(node5); + + + + assertEquals(3,n.size()); + assertEquals(3,n1.size()); + assertEquals(ExpectedUnion,n.Union(n1)); + assertEquals(ExpectedIntersection,n.Intersection(n1)); + assertEquals(ExpectedDiff,n.difference(n1)); + + } + + + + //NEW TEST + @Test + public void testVariableSet() { + + VariableSet v = new VariableSet(); + Variable v1 = new Variable("v1"); + Variable v2 = new Variable("v2"); + Variable v3 = new Variable("v3"); + + v.addVariable(v1); + v.addVariable(v2); + v.addVariable(v3); + + assertEquals(3,v.size()); + assertTrue(v.contains(v1)); + + v.removeVariable(v1); + + assertEquals(2,v.size()); + assertFalse(v.contains(v1)); + + } + + + + private Object[] buildNodesForUnitPathTesting() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, CannotBuildNodeException, EquivalentNodeException, CaseFrameMissMatchException, SemanticNotFoundInNetworkException { + // create relations + Relation relation = Network.defineRelation("relation", semanticType); + LinkedList relations = new LinkedList(); + relations.add(relation); + + // define case frame + CaseFrame testCF = Network.defineCaseFrame(semanticType, relations); + + // build base node + Node base = Network.buildBaseNode("b", semantic); + + // create wires + ArrayList wires = new ArrayList(); + wires.add(new Wire(relation, base)); + + // create parent node + Node parent = Network.buildMolecularNode(wires, testCF); + + Object[] result = {relation, testCF, base, parent}; + return result; + } + + private Object[] buildNodesForCFResUnitPathTesting() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, CannotBuildNodeException, EquivalentNodeException, CaseFrameMissMatchException, SemanticNotFoundInNetworkException { + // create relations + Relation relation = Network.defineRelation("relation", semanticType); + LinkedList relations = new LinkedList(); + relations.add(relation); + + // define case frames + CaseFrame testCF = Network.defineCaseFrame(semanticType, relations); + relations = new LinkedList(); + relations.add(relation); + relations.add(Network.defineRelation("relation2", semanticType)); + CaseFrame otherCF = Network.defineCaseFrame(semanticType, relations); + + // build base node + Node base = Network.buildBaseNode("b", semantic); + + // create wires + ArrayList wires = new ArrayList(); + wires.add(new Wire(relation, base)); + + // create parent node + Node parent = Network.buildMolecularNode(wires, testCF); + + Object[] result = {relation, testCF, base, parent, otherCF}; + return result; + } + + + private Object[] buildNetworkForKPlusAndKStarTesting() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, CannotBuildNodeException, EquivalentNodeException, CaseFrameMissMatchException, SemanticNotFoundInNetworkException { + // create relations + Relation relation = Network.defineRelation("rel1", semanticType); + LinkedList relations = new LinkedList(); + relations.add(relation); + + // define the case frame + CaseFrame testCF = Network.defineCaseFrame(semanticType, relations); + + // build base node + Node base = Network.buildBaseNode("b", semantic); + + // create wires for parent + ArrayList wires = new ArrayList(); + wires.add(new Wire(relation, base)); + + // create parent node + Node parent = Network.buildMolecularNode(wires, testCF); + + // create wires for grandparent + wires.clear(); + wires.add(new Wire(relation, parent)); + + // create grandparent node + Node grandparent = Network.buildMolecularNode(wires, testCF); + + // return the network + Object[] result = {relation, testCF, base, parent, grandparent}; + return result; + } + + @Test + public void testFUnitPath() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, CannotBuildNodeException, EquivalentNodeException, CaseFrameMissMatchException, SemanticNotFoundInNetworkException { + Object[] testNetwork = buildNodesForUnitPathTesting(); + Relation relation = (Relation) testNetwork[0]; + Node base = (Node) testNetwork[2]; + Node parent = (Node) testNetwork[3]; + + + // create the path and follow it + FUnitPath path = new FUnitPath(relation); + LinkedList nodes = path.follow(parent, new PathTrace(), Controller.getCurrentContext()); + + assertEquals(nodes.size(), 1); + assertEquals(( (Node) nodes.get(0)[0]).getId(), base.getId()); + + nodes = path.followConverse(parent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(),0); + + // follow the converse of the path + nodes = path.followConverse(base, new PathTrace(), Controller.getCurrentContext()); + + assertEquals(nodes.size(), 1); + assertEquals(( (Node) nodes.get(0)[0]).getId(), parent.getId()); + + nodes = path.follow(base, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 0); + } + + @Test + public void testBUnitPath() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, CannotBuildNodeException, EquivalentNodeException, CaseFrameMissMatchException, SemanticNotFoundInNetworkException { + Object[] testNetwork = buildNodesForUnitPathTesting(); + Relation relation = (Relation) testNetwork[0]; + Node base = (Node) testNetwork[2]; + Node parent = (Node) testNetwork[3]; + + + // create path and follow it + BUnitPath path = new BUnitPath(relation); + LinkedList nodes = path.follow(base, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 1); + assertEquals(( (Node) nodes.get(0)[0]).getId(), parent.getId()); + + nodes = path.follow(parent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 0); + + // follow the converse of the path + nodes = path.followConverse(parent, new PathTrace(), Controller.getCurrentContext()); + + assertEquals(nodes.size(), 1); + assertEquals(( (Node) nodes.get(0)[0]).getId(), base.getId()); + + nodes = path.followConverse(base, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(),0); + } + + @Test + public void testCFResBUnitPath() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, CannotBuildNodeException, EquivalentNodeException, CaseFrameMissMatchException, SemanticNotFoundInNetworkException { + Object[] testNetwork = buildNodesForCFResUnitPathTesting(); + Relation relation = (Relation) testNetwork[0]; + CaseFrame testCF = (CaseFrame) testNetwork[1]; + Node base = (Node) testNetwork[2]; + Node parent = (Node) testNetwork[3]; + CaseFrame otherCF = (CaseFrame) testNetwork[4]; + + // create the path and follow it + CFResBUnitPath path = new CFResBUnitPath(relation, testCF); + LinkedList nodes = path.follow(base, new PathTrace(), Controller.getCurrentContext()); + + assertEquals(nodes.size(), 1); + assertEquals(( (Node) nodes.get(0)[0]).getId(), parent.getId()); + + nodes = path.followConverse(base, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(),0); + + // follow the converse of the path + nodes = path.followConverse(parent, new PathTrace(), Controller.getCurrentContext()); + + assertEquals(nodes.size(), 1); + assertEquals(( (Node) nodes.get(0)[0]).getId(), base.getId()); + + nodes = path.follow(parent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 0); + + // follow a path with a different case frame + path = new CFResBUnitPath(relation, otherCF); + nodes = path.follow(base, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 0); + + nodes = path.follow(parent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 0); + + nodes = path.followConverse(parent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(),0); + + nodes = path.followConverse(base, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(),0); + } + + @Test + public void testCFResFUnitPath() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, CannotBuildNodeException, EquivalentNodeException, CaseFrameMissMatchException, SemanticNotFoundInNetworkException { + Object[] testNetwork = buildNodesForCFResUnitPathTesting(); + Relation relation = (Relation) testNetwork[0]; + CaseFrame testCF = (CaseFrame) testNetwork[1]; + Node base = (Node) testNetwork[2]; + Node parent = (Node) testNetwork[3]; + CaseFrame otherCF = (CaseFrame) testNetwork[4]; + + // create the path and follow it + CFResFUnitPath path = new CFResFUnitPath(relation, testCF); + LinkedList nodes = path.follow(parent, new PathTrace(), Controller.getCurrentContext()); + + assertEquals(nodes.size(), 1); + assertEquals(( (Node) nodes.get(0)[0]).getId(), base.getId()); + + nodes = path.followConverse(parent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(),0); + + // follow the converse of the path + nodes = path.followConverse(base, new PathTrace(), Controller.getCurrentContext()); + + assertEquals(nodes.size(), 1); + assertEquals(( (Node) nodes.get(0)[0]).getId(), parent.getId()); + + nodes = path.follow(base, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 0); + + // follow a path with a different case frame + path = new CFResFUnitPath(relation, otherCF); + nodes = path.follow(parent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 0); + + nodes = path.follow(base, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 0); + + nodes = path.followConverse(base, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(),0); + + nodes = path.followConverse(parent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(),0); + } + + + @Test + public void testKPlusPath() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, CannotBuildNodeException, EquivalentNodeException, CaseFrameMissMatchException, SemanticNotFoundInNetworkException { + boolean parentFound = false, grandparentFound = false, baseFound = false; + Object[] network = buildNetworkForKPlusAndKStarTesting(); + Relation relation = (Relation) network[0]; + Node base = (Node) network[2]; + Node parent = (Node) network[3]; + Node grandparent = (Node) network[4]; + + // create the path and use it to create a KStarPath + FUnitPath FU = new FUnitPath(relation); + KPlusPath path = new KPlusPath(FU); + + // follow the path from the grandparent + LinkedList nodes = path.follow(grandparent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 2); + + for(int i = 0; i < 2; i++) { + Node current = (Node) nodes.get(i)[0]; + if(current.getId() == base.getId()) + baseFound = true; + if(current.getId() == parent.getId()) + parentFound = true; + if(current.getId() == grandparent.getId()) + grandparentFound = true; + } + + assertTrue(baseFound && parentFound && !grandparentFound); + baseFound = parentFound = grandparentFound = false; + + nodes = path.followConverse(grandparent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 0); + + // follow the path from the parent + nodes = path.follow(parent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 1); + + for(int i = 0; i < 1; i++) { + Node current = (Node) nodes.get(i)[0]; + if(current.getId() == base.getId()) + baseFound = true; + if(current.getId() == parent.getId()) + parentFound = true; + if(current.getId() == grandparent.getId()) + grandparentFound = true; + } + + assertTrue(baseFound && !parentFound && !grandparentFound); + baseFound = parentFound = grandparentFound = false; + + nodes = path.followConverse(parent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 1); + + for(int i = 0; i < 1; i++) { + Node current = (Node) nodes.get(i)[0]; + if(current.getId() == base.getId()) + baseFound = true; + if(current.getId() == parent.getId()) + parentFound = true; + if(current.getId() == grandparent.getId()) + grandparentFound = true; + } + + assertTrue(!baseFound && !parentFound && grandparentFound); + baseFound = parentFound = grandparentFound = false; + + // follow the path from the base + nodes = path.followConverse(base, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 2); + + for(int i = 0; i < 2; i++) { + Node current = (Node) nodes.get(i)[0]; + if(current.getId() == base.getId()) + baseFound = true; + if(current.getId() == parent.getId()) + parentFound = true; + if(current.getId() == grandparent.getId()) + grandparentFound = true; + } + + assertTrue(!baseFound && parentFound && grandparentFound); + + nodes = path.follow(base, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 0); + } + + @Test + public void testKStarPath() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, CannotBuildNodeException, EquivalentNodeException, CaseFrameMissMatchException, SemanticNotFoundInNetworkException { + boolean parentFound = false, grandparentFound = false, baseFound = false; + Object[] network = buildNetworkForKPlusAndKStarTesting(); + Relation relation = (Relation) network[0]; + Node base = (Node) network[2]; + Node parent = (Node) network[3]; + Node grandparent = (Node) network[4]; + + // create the path and use it to create a KStarPath + FUnitPath FU = new FUnitPath(relation); + KStarPath path = new KStarPath(FU); + + // follow the path from the grandparent + LinkedList nodes = path.follow(grandparent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 3); + + for(int i = 0; i < 3; i++) { + Node current = (Node) nodes.get(i)[0]; + if(current.getId() == base.getId()) + baseFound = true; + if(current.getId() == parent.getId()) + parentFound = true; + if(current.getId() == grandparent.getId()) + grandparentFound = true; + } + + assertTrue(baseFound && parentFound && grandparentFound); + baseFound = parentFound = grandparentFound = false; + + nodes = path.followConverse(grandparent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 1); + assertEquals(((Node)nodes.get(0)[0]).getId(), grandparent.getId()); + + // follow the path from the parent + nodes = path.follow(parent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 2); + + for(int i = 0; i < 2; i++) { + Node current = (Node) nodes.get(i)[0]; + if(current.getId() == base.getId()) + baseFound = true; + if(current.getId() == parent.getId()) + parentFound = true; + if(current.getId() == grandparent.getId()) + grandparentFound = true; + } + + assertTrue(baseFound && parentFound && !grandparentFound); + baseFound = parentFound = grandparentFound = false; + + nodes = path.followConverse(parent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 2); + + for(int i = 0; i < 2; i++) { + Node current = (Node) nodes.get(i)[0]; + if(current.getId() == base.getId()) + baseFound = true; + if(current.getId() == parent.getId()) + parentFound = true; + if(current.getId() == grandparent.getId()) + grandparentFound = true; + } + + assertTrue(!baseFound && parentFound && grandparentFound); + baseFound = parentFound = grandparentFound = false; + + // follow the path from the base + nodes = path.followConverse(base, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 3); + + for(int i = 0; i < 3; i++) { + Node current = (Node) nodes.get(i)[0]; + if(current.getId() == base.getId()) + baseFound = true; + if(current.getId() == parent.getId()) + parentFound = true; + if(current.getId() == grandparent.getId()) + grandparentFound = true; + } + + assertTrue(baseFound && parentFound && grandparentFound); + + nodes = path.follow(base, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 1); + assertEquals(((Node)nodes.get(0)[0]).getId(), base.getId()); + } + + + @Test + public void testOrPath() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, CannotBuildNodeException, EquivalentNodeException, CaseFrameMissMatchException, SemanticNotFoundInNetworkException { + Network.clearNetwork(); + // create the relations + Relation relation1 = Network.defineRelation("rel1", semanticType); + Relation relation2 = Network.defineRelation("rel2", semanticType); + LinkedList relationSet = new LinkedList(); + relationSet.add(relation1); + relationSet.add(relation2); + + // define case frame + CaseFrame caseFrame = Network.defineCaseFrame(semanticType, relationSet); + + // build the base nodes + Node base1 = Network.buildBaseNode("b1", semantic); + Node base2 = Network.buildBaseNode("b2", semantic); + + // create wires + ArrayList wires = new ArrayList(); + wires.add(new Wire(relation1, base1)); + wires.add(new Wire(relation2, base2)); + + // build the molecular node + Node parent = Network.buildMolecularNode(wires, caseFrame); + + // build the paths and use them to build an or path + FUnitPath path1 = new FUnitPath(relation1); + FUnitPath path2 = new FUnitPath(relation2); + LinkedList paths = new LinkedList(); + paths.add(path1); + paths.add(path2); + OrPath orPath = new OrPath(paths); + + // follow the path from the parent + LinkedList nodes = orPath.follow(parent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 2); + assertTrue( ( ((Node)nodes.get(0)[0]).getId() == (base1.getId()) || ((Node)nodes.get(0)[0]).getId() == (base2.getId()) ) + && ( ((Node)nodes.get(1)[0]).getId() == (base1.getId()) || ((Node)nodes.get(1)[0]).getId() == (base2.getId()) ) ); + + // follow the converse from base 1 + nodes = orPath.followConverse(base1, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 1); + assertEquals(((Node)nodes.get(0)[0]).getId(), parent.getId()); + + // follow the converse from base 2 + nodes = orPath.followConverse(base2, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 1); + assertEquals(((Node)nodes.get(0)[0]).getId(), parent.getId()); + } + + @Test + public void testAndPath() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, CannotBuildNodeException, EquivalentNodeException, CaseFrameMissMatchException, SemanticNotFoundInNetworkException { + // create the relations + Relation relation1 = Network.defineRelation("rel1", semanticType); + Relation relation2 = Network.defineRelation("rel2", semanticType); + LinkedList relationSet = new LinkedList(); + relationSet.add(relation1); + relationSet.add(relation2); + + // define case frame + CaseFrame caseFrame = Network.defineCaseFrame(semanticType, relationSet); + + // build the base node + Node base = Network.buildBaseNode("b1", semantic); + + // create wires + ArrayList wires = new ArrayList(); + wires.add(new Wire(relation1, base)); + wires.add(new Wire(relation2, base)); + + // build the molecular node + Node parent = Network.buildMolecularNode(wires, caseFrame); + + // build the paths and use them to build an and path + FUnitPath path1 = new FUnitPath(relation1); + FUnitPath path2 = new FUnitPath(relation2); + LinkedList paths = new LinkedList(); + paths.add(path1); + paths.add(path2); + AndPath andPath = new AndPath(paths); + + // follow the path from the parent + LinkedList nodes = andPath.follow(parent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 1); + assertEquals(base.getId(), ( (Node) nodes.get(0)[0]).getId()); + + // follow the path from the base + nodes = andPath.follow(base, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 0); + + // follow the converse of the path from the parent + nodes = andPath.followConverse(parent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 0); + + // follow the converse of the path from the base + nodes = andPath.followConverse(base, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 1); + assertEquals(parent.getId(), ( (Node) nodes.get(0)[0]).getId()); + + // create a new base node + Node base2 = Network.buildBaseNode("b2", semantic); + + // create new wire list + wires.clear(); + wires.add(new Wire(relation1, base)); + wires.add(new Wire(relation2, base2)); + + // create new parent + parent = Network.buildMolecularNode(wires, caseFrame); + + // follow the path from the parent + nodes = andPath.follow(parent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 0); + } + + + + + + @Test + public void testConversePath() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, CannotBuildNodeException, EquivalentNodeException, CaseFrameMissMatchException, SemanticNotFoundInNetworkException { + Object[] testNetwork = buildNodesForUnitPathTesting(); + Relation relation = (Relation) testNetwork[0]; + Node base = (Node) testNetwork[2]; + Node parent = (Node) testNetwork[3]; + + + // create path and follow it + BUnitPath BUPath = new BUnitPath(relation); + ConversePath path = new ConversePath(BUPath); + LinkedList nodes = path.follow(parent, new PathTrace(), Controller.getCurrentContext()); + + assertEquals(nodes.size(), 1); + assertEquals(( (Node) nodes.get(0)[0]).getId(), base.getId()); + + nodes = path.followConverse(parent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(),0); + + // follow the converse of the path + nodes = path.followConverse(base, new PathTrace(), Controller.getCurrentContext()); + + assertEquals(nodes.size(), 1); + assertEquals(( (Node) nodes.get(0)[0]).getId(), parent.getId()); + + nodes = path.follow(base, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 0); + } + + + @Test + public void testIrreflexiveRestrictPath() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, CannotBuildNodeException, EquivalentNodeException, CaseFrameMissMatchException, SemanticNotFoundInNetworkException { + boolean parentFound = false, grandparentFound = false, baseFound = false; + Object[] network = buildNetworkForKPlusAndKStarTesting(); + Relation relation = (Relation) network[0]; + Node base = (Node) network[2]; + Node parent = (Node) network[3]; + Node grandparent = (Node) network[4]; + + // create the path and use it to create a KStarPath + FUnitPath FU = new FUnitPath(relation); + KStarPath KStarPath = new KStarPath(FU); + IrreflexiveRestrictPath path = new IrreflexiveRestrictPath(KStarPath); + + // follow the path from the grandparent + LinkedList nodes = path.follow(grandparent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 2); + + for(int i = 0; i < 2; i++) { + Node current = (Node) nodes.get(i)[0]; + if(current.getId() == base.getId()) + baseFound = true; + if(current.getId() == parent.getId()) + parentFound = true; + if(current.getId() == grandparent.getId()) + grandparentFound = true; + } + + assertTrue(baseFound && parentFound && !grandparentFound); + } + + @Test + public void testComposePath() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, CannotBuildNodeException, EquivalentNodeException, CaseFrameMissMatchException, SemanticNotFoundInNetworkException { + // define relations + Relation relation1, relation2; + relation1 = Network.defineRelation("relation1", semanticType); + relation2 = Network.defineRelation("relation2", semanticType); + LinkedList relations1 = new LinkedList(); + LinkedList relations2 = new LinkedList(); + relations1.add(relation1); + relations2.add(relation2); + + // define case frames + CaseFrame cf1, cf2; + cf1 = Network.defineCaseFrame(semanticType, relations1); + cf2 = Network.defineCaseFrame(semanticType, relations2); + + // build the base node + Node base = Network.buildBaseNode("b1", semantic); + + // build a parent + ArrayList wires = new ArrayList(); + wires.add(new Wire(relation1, base)); + + Node parent = Network.buildMolecularNode(wires, cf1); + + // build a grandparent + wires.clear(); + wires.add(new Wire(relation2, parent)); + + Node grandparent = Network.buildMolecularNode(wires, cf2); + + // build the path + FUnitPath fPath1 = new FUnitPath(relation1); + FUnitPath fPath2 = new FUnitPath(relation2); + LinkedList paths = new LinkedList(); + paths.add(fPath2); + paths.add(fPath1); + ComposePath path = new ComposePath(paths); + + // follow the path from the grandparent + LinkedList nodes = path.follow(grandparent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 1); + assertEquals(( (Node) nodes.get(0)[0]).getId(), base.getId()); + + nodes = path.followConverse(grandparent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 0); + + nodes = path.follow(base, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 0); + + nodes = path.followConverse(base, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 1); + assertEquals(( (Node) nodes.get(0)[0]).getId(), grandparent.getId()); + } + + @Test + public void testDomainRestrictPath() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, CannotBuildNodeException, EquivalentNodeException, CaseFrameMissMatchException, SemanticNotFoundInNetworkException { + Network.clearNetwork(); + // create relations + Relation relation1 = Network.defineRelation("relation1", semanticType); + Relation relation2 = Network.defineRelation("relation2", semanticType); + LinkedList relations = new LinkedList(); + relations.add(relation1); + relations.add(relation2); + + // define case frame + CaseFrame caseFrame = Network.defineCaseFrame(semanticType, relations); + + // build two base nodes + Node base = Network.buildBaseNode("b1", semantic); + Node zNode = Network.buildBaseNode("zNode", semantic); + + // build the parent + ArrayList wires = new ArrayList(); + wires.add(new Wire(relation1, base)); + wires.add(new Wire(relation2, zNode)); + Node parent = Network.buildMolecularNode(wires, caseFrame); + + // create the paths + FUnitPath p = new FUnitPath(relation1); + FUnitPath q = new FUnitPath(relation2); + DomainRestrictPath path = new DomainRestrictPath(q, zNode, p); + + // follow the path from the parent + LinkedList nodes = path.follow(parent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 1); + assertEquals(((Node)nodes.get(0)[0]).getId(), base.getId()); + + nodes = path.followConverse(parent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 0); + + // follow the path from the base + nodes = path.followConverse(base, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 1); + assertEquals(((Node)nodes.get(0)[0]).getId(), parent.getId()); + + nodes = path.follow(base, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 0); + } + + @Test + public void testRangeRestrictPath() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, CannotBuildNodeException, EquivalentNodeException, CaseFrameMissMatchException, SemanticNotFoundInNetworkException { + Object[] network = buildNetworkForKPlusAndKStarTesting(); + Relation relation = (Relation) network[0]; + Node base = (Node) network[2]; + Node parent = (Node) network[3]; + Node grandparent = (Node) network[4]; + + // create the paths + FUnitPath p = new FUnitPath(relation); + FUnitPath q = new FUnitPath(relation); + RangeRestrictPath path = new RangeRestrictPath(p, q, base); + + // follow the path from the grandparent + LinkedList nodes = path.follow(grandparent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 1); + assertEquals(((Node)nodes.get(0)[0]).getId(), parent.getId()); + + nodes = path.followConverse(grandparent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 0); + + // follow the path from the parent + nodes = path.followConverse(parent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 1); + assertEquals(((Node)nodes.get(0)[0]).getId(), grandparent.getId()); + + nodes = path.follow(parent, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 0); + } + + @Test + public void testBangPath() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, SemanticNotFoundInNetworkException, DuplicatePropositionException, ContradictionFoundException, ContextNameDoesntExistException { + Node node = Network.buildBaseNode("node", SemanticHierarchy.getSemantic(semanticType)); + + BangPath bPath = new BangPath(); + + // follow the bang path form a non-asserted node + LinkedList nodes = bPath.follow(node, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 0); + + // assert the node + Controller.addPropToCurrentContext(node.getId()); + + // follow the bang path from an asserted node + nodes = bPath.follow(node, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 1); + assertEquals( ((Node) nodes.get(0)[0]).getId() , node.getId()); + + // follow the converse of the bPath + nodes = bPath.follow(node, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 1); + assertEquals( ((Node) nodes.get(0)[0]).getId() , node.getId()); + + // create a new node that has a semantic type that is a child of proposition + SemanticHierarchy.createSemanticType("semantic", semanticType); + node = Network.buildBaseNode("node2", SemanticHierarchy.getSemantic("semantic")); + + // assert the node + Controller.addPropToCurrentContext(node.getId()); + + // follow the band path from the asserted node + nodes = bPath.follow(node, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 1); + assertEquals( ((Node) nodes.get(0)[0]).getId() , node.getId()); + + // follow the converse of the bPath + nodes = bPath.follow(node, new PathTrace(), Controller.getCurrentContext()); + assertEquals(nodes.size(), 1); + assertEquals( ((Node) nodes.get(0)[0]).getId() , node.getId()); + } + + + @Test + public void testRemoveIsolatedBaseNode() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, SemanticNotFoundInNetworkException, NodeCannotBeRemovedException, NodeNotFoundInPropSetException { + Network.clearNetwork(); + // create an isolated base node + Network.buildBaseNode("node", semantic); + assertEquals(Network.getNodes().size(), 1); + + Network.removeNode(Network.getNode("node")); + assertEquals(Network.getNodes().size(), 0); + + try { + Network.getNode("node"); + } + catch(Exception e) { + assertEquals(e.getMessage(), "There is no node named 'node' in the network"); + } + } + + @Test + public void testRemoveNonIsolatedNode() throws NotAPropositionNodeException, NodeNotFoundInNetworkException, IllegalIdentifierException, SemanticNotFoundInNetworkException, CannotBuildNodeException, EquivalentNodeException, CaseFrameMissMatchException { + + Network.clearNetwork(); + Relation relation = Network.defineRelation("relation1", semanticType); + + LinkedList relList = new LinkedList(); + relList.add(relation); + + CaseFrame caseFrame = Network.defineCaseFrame(semanticType, relList); + + Node nonIsolatedNode = Network.buildBaseNode("b1", SemanticHierarchy.getSemantic(semanticType)); + + ArrayList wires = new ArrayList(); + wires.add(new Wire(relation, nonIsolatedNode)); + + Network.buildMolecularNode(wires, caseFrame); + + try { + Network.removeNode(nonIsolatedNode); + } + catch (Exception e) { + assertEquals(e.getMessage(), "Cannot remove the node named 'b1' because it is not isolated"); + assertEquals(Network.getNodes().size(), 2); + } + } + + + +} + + + + + + + From 102b83ffbc1e6ba37dd6afe4463b822681b6e4c5 Mon Sep 17 00:00:00 2001 From: Omarnael <47508294+Omarnael@users.noreply.github.com> Date: Wed, 15 Jul 2020 17:07:11 +0200 Subject: [PATCH 2/4] Update Network.java Network Class Modifications --- src/sneps/network/Network.java | 681 ++++++++++++++++++--------------- 1 file changed, 379 insertions(+), 302 deletions(-) diff --git a/src/sneps/network/Network.java b/src/sneps/network/Network.java index 184e4550..dcfefcc1 100644 --- a/src/sneps/network/Network.java +++ b/src/sneps/network/Network.java @@ -13,6 +13,7 @@ import java.util.Enumeration; import java.util.Hashtable; import java.util.LinkedList; +import java.util.Set; import sneps.exceptions.*; import sneps.network.cables.Cable; @@ -72,7 +73,7 @@ public class Network implements Serializable { * an array list that stores all the nodes defined in the network. Each node is * stored in the array list at the position corresponding to its ID. */ - private static ArrayList nodesIndex = new ArrayList(); + public static ArrayList nodesIndex = new ArrayList(); /** * A has hash table that contains all the molecular nodes defined in the network @@ -146,7 +147,7 @@ public static Hashtable getNodes() { /** * This is created to reduce the search space when searching for only * proposition nodes - * + * * @return the hash table that stores the proposition nodes defined in the * network. */ @@ -216,12 +217,11 @@ public static LinkedList getUserDefinedVarSuffix() { /** * - * @param name - * the name of the relation that will be retrieved. + * @param name the name of the relation that will be retrieved. * * @return the relation with the specified name if it exists. - * @throws RelationDoesntExistException - * if the requested relation does not exist. + * @throws RelationDoesntExistException if the requested relation does not + * exist. */ public static Relation getRelation(String name) throws RelationDoesntExistException { if (relations.containsKey(name)) { @@ -233,13 +233,12 @@ public static Relation getRelation(String name) throws RelationDoesntExistExcept /** * - * @param id - * the string id of the case frame that will be retrieved. + * @param id the string id of the case frame that will be retrieved. * * @return the case frame with the specified id if it exists. * - * @throws CaseFrameWithSetOfRelationsNotFoundException - * if the requested frame does not exist. + * @throws CaseFrameWithSetOfRelationsNotFoundException if the requested frame + * does not exist. */ public static CaseFrame getCaseFrame(String id) throws CaseFrameWithSetOfRelationsNotFoundException { if (caseFrames.containsKey(id)) { @@ -252,13 +251,11 @@ public static CaseFrame getCaseFrame(String id) throws CaseFrameWithSetOfRelatio /** * - * @param identifier - * the name of the node that will be retrieved. + * @param identifier the name of the node that will be retrieved. * * @return the node with the specified name if it exists. * - * @throws NodeNotFoundInNetworkException - * if the requested node does not exist. + * @throws NodeNotFoundInNetworkException if the requested node does not exist. */ public static Node getNode(String identifier) throws NodeNotFoundInNetworkException { if (nodes.containsKey(identifier)) { @@ -279,22 +276,17 @@ public static Node getNodeById(int id) throws NodeNotFoundInNetworkException { /** * This method is used to define a new relation in the network. * - * @param name - * the name of the new relation. - * @param type - * the name of the semantic class that specify the semantic of the - * nodes that this new relation can point to. - * @param adjust - * the adjustability of the new relation. - * @param limit - * the minimum number of nodes that this new relation can point to - * within a down-cable. + * @param name the name of the new relation. + * @param type the name of the semantic class that specify the semantic of the + * nodes that this new relation can point to. + * @param adjust the adjustability of the new relation. + * @param limit the minimum number of nodes that this new relation can point to + * within a down-cable. * * @return the newly created relation. * - * @throws CustomException - * if another relation with the same given name is already defined - * in the network. + * @throws CustomException if another relation with the same given name is + * already defined in the network. */ public static Relation defineRelation(String name, String type, String adjust, int limit) { if (relations.containsKey(name)) { @@ -321,11 +313,10 @@ public static Relation defineRelation(String name, String type) { /** * This method is used to delete a relation from the network. * - * @param name - * the name of the relation that will be deleted. - * @throws CaseFrameCannotBeRemovedException - * if the relation cannot be removed because one of the case frames - * that contains it cannot be removed. + * @param name the name of the relation that will be deleted. + * @throws CaseFrameCannotBeRemovedException if the relation cannot be removed + * because one of the case frames that + * contains it cannot be removed. */ public static void undefineRelation(String name) throws CaseFrameCannotBeRemovedException { Relation r = relations.get(name); @@ -349,18 +340,33 @@ public static void undefineRelation(String name) throws CaseFrameCannotBeRemoved /** * This method is used to define a new case frame. * - * @param semanticType - * the default semantic type specified by the new case frame. - * @param relationSet - * the list that contains the RCFP's of the relations included in the - * new case frame. + * @param semanticType the default semantic type specified by the new case + * frame. + * @param relationSet the list that contains the RCFP's of the relations + * included in the new case frame. * * @return the newly created case frame. * */ public static RelationsRestrictedCaseFrame defineCaseFrameWithConstraints(String semanticType, LinkedList relationSet) { + + + RelationsRestrictedCaseFrame caseFrame = new RelationsRestrictedCaseFrame(semanticType, relationSet); + + LinkedList result = CheckCFConflicts(caseFrame); + if(result == null) { + return (RelationsRestrictedCaseFrame) caseFrames.get(caseFrame.getId()); + } + + if (result.size() > 0) { + System.out.println(result); + return null; + } + + else { + if (caseFrames.containsKey(caseFrame.getId())) { return (RelationsRestrictedCaseFrame) caseFrames.get(caseFrame.getId()); } else { @@ -369,6 +375,7 @@ public static RelationsRestrictedCaseFrame defineCaseFrameWithConstraints(String if (!molecularNodes.containsKey(caseFrame.getId())) molecularNodes.put(caseFrame.getId(), new NodeSet()); } + } return (RelationsRestrictedCaseFrame) caseFrames.get(caseFrame.getId()); } @@ -389,13 +396,12 @@ public static CaseFrame defineCaseFrame(String semanticType, LinkedList wires, CaseFrame caseFrame // if an equivalent node is found, it is returned and no new node is built. Node equivalentNodeInNetwork = downCableSetExists(array); // System.out.println("Downcable set exists > "+ downCableSetExists(array)); - if (equivalentNodeInNetwork != null) { return equivalentNodeInNetwork; } @@ -661,7 +668,7 @@ public static Node buildMolecularNode(ArrayList wires, CaseFrame caseFrame throw new CaseFrameMissMatchException( "Not following the case frame .. wrong node set size or wrong set of relations"); // create the Molecular Node - if (caseFrame.getSemanticClass().equals("Proposition") + if (caseFrame.getSemanticClass().equals("Proposition") || SemanticHierarchy.getSemantic(caseFrame.getSemanticClass()).getSuperClassesNames().contains("Proposition")) { PropositionNode propNode; if (isToBePattern(array)) { @@ -703,12 +710,13 @@ public static Node buildMolecularNode(ArrayList wires, RelationsRestricted // if an equivalent node is found, it is returned and no new node is built. Node equivalentNodeInNetwork = downCableSetExists(array); // System.out.println("Downcable set exists > "+ downCableSetExists(array)); - + if (equivalentNodeInNetwork != null) { return equivalentNodeInNetwork; } // check the validity of the relation-node pairs // System.out.println("done 1st"); + if (!validRelNodePairs(array)) throw new CannotBuildNodeException("Cannot build the node .. the relation node pairs are not valid"); // System.out.println("done 2nd"); @@ -727,6 +735,7 @@ public static Node buildMolecularNode(ArrayList wires, RelationsRestricted propNode = (PropositionNode) createPatNode(relNodeSet, caseFrame); } else { // System.out.println("building closed"); + propNode = (PropositionNode) createClosedNode(relNodeSet, caseFrame); } nodes.put(propNode.getIdentifier(), propNode); @@ -752,15 +761,49 @@ public static Node buildMolecularNode(ArrayList wires, RelationsRestricted return mNode; } } + + static ArrayList DupNodes = new ArrayList(); + + public static void setBaseNodeType(Object[][] array) { + + + for (int i = 0; i= 1) { + System.out.println(">1"); + System.out.println(n.getUpCableSet().getUpCables().keySet().toString()); + String sem = n.getUpCableSet().getUpCables().keySet().toString(); + + } + + } + } + } + System.out.println(DupNodes); +} /** * checks whether the given down cable set already exists in the network or not. * - * @param array - * a 2D array of Relation-Node pairs representing a down cable set - * specifications. + * @param array a 2D array of Relation-Node pairs representing a down cable set + * specifications. * - * @return the node that has this DownCableSet if it is found, or the node + * @return the node that has this DownCableSet if it is found, or the node * that has an equivalent DownCableSet if it is found. Returns null otherwise */ private static Node downCableSetExists(Object[][] array) { @@ -871,7 +914,7 @@ private static Node downCableSetExists(Object[][] array) { } else { result = false; } - + if(result) { // if given DownCableSet exists within the network, return the node that this DownCableSet belongs to return (Node) ns.get(0)[0]; @@ -887,11 +930,10 @@ private static Node downCableSetExists(Object[][] array) { * valid. The pair is valid if the relation can point to the node paired with it * according to the semantic type specified in the relation. In the current * implementation any relation can point to the variable node because all nodes - * have infimum as their semantic type. + * have Entity as their semantic type. * - * @param array - * a 2D array of Relation-Node pairs that represents the - * specifications of the down cable set of a new molecular node. + * @param array a 2D array of Relation-Node pairs that represents the + * specifications of the down cable set of a new molecular node. * * @return true if each pair in the 2D array is valid, and false otherwise. */ @@ -913,7 +955,7 @@ public static boolean validRelNodePairs(Object[][] array) { return true; } - private static Object[][] turnWiresIntoArray(ArrayList wires) { + public static Object[][] turnWiresIntoArray(ArrayList wires) { Object[][] result = new Object[wires.size()][2]; for (int i = 0; i < result.length; i++) { @@ -925,7 +967,7 @@ private static Object[][] turnWiresIntoArray(ArrayList wires) { return result; } - private static Object[][] turnIntoRelNodeSet(Object[][] array) { + public static Object[][] turnIntoRelNodeSet(Object[][] array) { Object[][] temp = new Object[array.length][]; for (int i = 0; i < array.length; i++) { temp[i] = Arrays.copyOf(array[i], array[i].length); @@ -997,8 +1039,8 @@ private static boolean followingCaseFrame(Object[][] array, RelationsRestrictedC return false; return true; } - - @SuppressWarnings("unchecked") + + /*@SuppressWarnings("unchecked") private static boolean followingCaseFrame(Object[][] array, CaseFrame caseFrame) { LinkedList list = (LinkedList) caseFrame.getRelations().clone(); for (int i = 0; i < array.length; i++) { @@ -1016,6 +1058,27 @@ private static boolean followingCaseFrame(Object[][] array, CaseFrame caseFrame) if (!list.isEmpty()) return false; return true; + }*/ + + private static boolean followingCaseFrame(Object[][] array, CaseFrame caseFrame) { + LinkedList list = new LinkedList(); + list.addAll(caseFrame.getRelations()); + for (int i = 0; i < array.length; i++) { + Relation r = (Relation) array[i][0]; + if (list.contains(r)) { + if (((NodeSet) array[i][1]).size() >= r.getLimit()) { + list.remove(r); + } else { + return false; + } + } else { + return false; + } + } + if (!list.isEmpty()) { + return false; + } + return true; } /** @@ -1023,9 +1086,8 @@ private static boolean followingCaseFrame(Object[][] array, CaseFrame caseFrame) * whether it dominate free variables or not. Pattern nodes dominate free * variables while closed nodes do not dominate free variables. * - * @param array - * a 2D array of Relation-Node pairs that represents the - * specifications of the down cable set of the new molecular node. + * @param array a 2D array of Relation-Node pairs that represents the + * specifications of the down cable set of the new molecular node. * * @return true if the node dominates free variable and thus should be pattern * node, and false otherwise. @@ -1064,21 +1126,20 @@ private static boolean isToBePattern(Object[][] array) { * This method builds a new pattern node or proposition node with the given down * cable set specifications and case frame. * - * @param relNodeSet - * a 2D array of relation-nodeSet pairs that represents the down - * cable set of the new pattern or proposition node. - * @param caseFrame - * the case frame implemented by the new pattern or proposition node. + * @param relNodeSet a 2D array of relation-nodeSet pairs that represents the + * down cable set of the new pattern or proposition node. + * @param caseFrame the case frame implemented by the new pattern or + * proposition node. * * @return the newly created pattern node or proposition node. * - * @throws Exception - * if the semantic class specified by the case frame was not - * successfully created and thus the node was not built. + * @throws Exception if the semantic class specified by the case frame was not + * successfully created and thus the node was not built. */ @SuppressWarnings("rawtypes") private static Node createPatNode(Object[][] relNodeSet, CaseFrame caseFrame) { LinkedList dCables = new LinkedList(); + for (int i = 0; i < relNodeSet.length; i++) { dCables.add(new DownCable((Relation) relNodeSet[i][0], (NodeSet) relNodeSet[i][1])); } @@ -1162,17 +1223,15 @@ else if (caseFrame == RelationsRestrictedCaseFrame.whenDo) * set specifications and case frame. * * - * @param relNodeSet - * a 2D array of relation-nodeSet pairs that represents the down - * cable set of the new closed or proposition node. - * @param caseFrame - * the case frame implemented by the new closed or proposition node. + * @param relNodeSet a 2D array of relation-nodeSet pairs that represents the + * down cable set of the new closed or proposition node. + * @param caseFrame the case frame implemented by the new closed or proposition + * node. * * @return the newly created closed or proposition node. * - * @throws Exception - * if the semantic class specified by the case frame was not - * successfully created and thus the node was not built. + * @throws Exception if the semantic class specified by the case frame was not + * successfully created and thus the node was not built. */ @SuppressWarnings("rawtypes") private static Node createClosedNode(Object[][] relNodeSet, CaseFrame caseFrame) { @@ -1253,9 +1312,8 @@ private static Node createClosedNode(Object[][] relNodeSet, RelationsRestrictedC * the key and and the node set that contains the nodes pointed to by the * corresponding relation as the value. * - * @param relNodeSet - * a given 2D array of relation-nodeSet pairs that will be used to - * create the hash table + * @param relNodeSet a given 2D array of relation-nodeSet pairs that will be + * used to create the hash table * * @return the newly created hash table. */ @@ -1271,12 +1329,10 @@ public static Hashtable turnIntoHashtable(Object[][] relNodeSet * This method gets the case frame signature specified by the case frame based * on the down cable set of a certain node. * - * @param relNodeSet - * a hash table with entry having the relation name as the key and - * the node set of nodes pointed to by the corresponding relation as - * the value. - * @param caseframe - * a given case frame. + * @param relNodeSet a hash table with entry having the relation name as the key + * and the node set of nodes pointed to by the corresponding + * relation as the value. + * @param caseframe a given case frame. * * @return the (case frame signature) semantic type specified by the given case * frame based on the given down cable set specifications. @@ -1290,16 +1346,20 @@ public static String getCFSignature(Hashtable relNodeSet, Relat if (signatures.containsKey(currentId)) { LinkedList rules = (LinkedList) signatures.get(currentId) .getSubDomainConstraints().clone(); + for (int j = 0; j < rules.size(); j++) { SubDomainConstraint c = rules.get(j); + LinkedList checks = (LinkedList) c.getNodeChecks() .clone(); + NodeSet ns = relNodeSet.get(c.getRelation()); for (int k = 0; k < checks.size(); k++) { CableTypeConstraint check = checks.get(k); int counter = 0; + for (int l = 0; l < ns.size(); l++) { - if (ns.getNode(l).getSemanticType().equals(check.getSemanticType()) + if (ns.getNode(l).getSemanticType().toString().equals(check.getSemanticType()) || ns.getNode(l).getSemanticSuperClass().equals(check.getSemanticType())) { counter++; } @@ -1332,20 +1392,19 @@ public static String getCFSignature(Hashtable relNodeSet, Relat } } if (rules.isEmpty()) { - // System.out.println("Satisfied"); + System.out.println("Satisfied"); return signatures.get(currentId).getResultingType(); } } } - // System.out.println("Not Satisfied"); + + return caseframe.getSemanticClass(); } /** - * @param array - * a given 2D array that contains pairs of paths and node sets. - * @param context - * a given context. + * @param array a given 2D array that contains pairs of paths and node sets. + * @param context a given context. * * @return the node set of nodes that we can start following those paths in the * array from, in order to reach at least one node at each node set in @@ -1356,12 +1415,10 @@ public static LinkedList find(Object[][] array, Context context) { } /** - * @param array - * a given 2D array that contains pairs of paths and node sets. - * @param context - * a given context. - * @param index - * the index of the 2D array at which we should start traversing it. + * @param array a given 2D array that contains pairs of paths and node sets. + * @param context a given context. + * @param index the index of the 2D array at which we should start traversing + * it. * * @return the node set of nodes that we can start following those paths in the * array from, in order to reach at least one node of node sets at each @@ -1399,18 +1456,15 @@ private static LinkedList findIntersection(Object[][] array, Context c } else { result.addAll(findUnion(path, nodeSet, context)); } - // System.out.println("yeeeah"); + return result; } /** - * @param path - * the path that can be followed to get to one of the nodes - * specified. - * @param nodeSet - * the nodes that can be reached by following the path. - * @param context - * a given context. + * @param path the path that can be followed to get to one of the nodes + * specified. + * @param nodeSet the nodes that can be reached by following the path. + * @param context a given context. * @return a node set of nodes that we can start following the path from in * order to get to one of the nodes in the specified node set. */ @@ -1440,14 +1494,12 @@ private static LinkedList findUnion(Path path, NodeSet nodeSet, Contex /** * This method builds a new case frame signature with the given parameters. * - * @param result - * the name of the semantic class specified by the new case frame - * signature. - * @param rules - * the list of sub-domain constraints included in the new case frame - * signature. - * @param caseframeId - * the case frame if that this CFSignature will be included in + * @param result the name of the semantic class specified by the new case + * frame signature. + * @param rules the list of sub-domain constraints included in the new + * case frame signature. + * @param caseframeId the case frame if that this CFSignature will be included + * in * * @return the newly created case frame signature. */ @@ -1459,30 +1511,25 @@ public CFSignature createCFSignature(String result, LinkedList CheckCFConflicts(RCFPCaseFrame cf) { - // if (caseFrames.containsKey(cf.getId())) { - // return null; - // } + public static LinkedList CheckCFConflicts(RelationsRestrictedCaseFrame cf) { + if (caseFrames.containsKey(cf.getId())) { + return null; + } // // loop over all defined case frames - // Enumeration caseframes = caseFrames.elements(); - // LinkedList result = new LinkedList(); - // // looping on the case frames with supersets or subsets of relations - // while (caseframes.hasMoreElements()) { - // RCFPCaseFrame cf1 = caseframes.nextElement(); - // // get intersecting relations - // Hashtable intersection = getIntersectingRelations( - // cf1.getRelations(), cf.getRelations()); - // // if no intersecting relations not conflicting so skip - // if (intersection.size() == 0) { - // continue; - // } - // // check new case frame - // Enumeration relations = cf.getRelations().elements(); - // boolean satisfied = true; - // while (relations.hasMoreElements()) { - // RCFP r = relations.nextElement(); - // if (intersection.containsKey(r.getRelation().getName())) - // continue; - // if (r.getLimit() != 0) { - // satisfied = false; - // break; - // } - // } - // if (satisfied) { - // // check other case frame - // Enumeration relations1 = cf1.getRelations().elements(); - // boolean satisfied1 = true; - // while (relations1.hasMoreElements()) { - // RCFP r = relations1.nextElement(); - // if (intersection.containsKey(r.getRelation().getName())) - // continue; - // if (r.getLimit() != 0) { - // satisfied1 = false; - // break; - // } - // } - // if (satisfied1) { - // result.add(cf1); - // } - // } - // } - // return result; - // } + Enumeration caseframes = caseFrames.elements(); + LinkedList result = new LinkedList(); + // // looping on the case frames with supersets or subsets of relations + while (caseframes.hasMoreElements()) { + CaseFrame cf1 = caseframes.nextElement(); + // // get intersecting relations + LinkedList intersection = getIntersectingRelations( + cf1.getRelations(), cf.getRelations()); + // // if no intersecting relations not conflicting so skip + if (intersection.size() == 0) { + continue; + } + // // check new case frame + LinkedList relations = cf.getRelations(); + boolean satisfied = true; + for(int i = 0; i relations1 = cf1.getRelations(); + boolean satisfied1 = true; + for(int i = 0; i getIntersectingRelations(Hashtable list1, - Hashtable list2) { - Enumeration relations = list1.elements(); - Hashtable result = new Hashtable(); - while (relations.hasMoreElements()) { - RCFP r = relations.nextElement(); - if (list2.containsKey(r.getRelation().getName())) { - result.put(r.getRelation().getName(), r); + public static LinkedList getIntersectingRelations(LinkedList linkedList, + LinkedList linkedList2) { + + LinkedList relations = linkedList; + + LinkedList result = new LinkedList(); + for(int i = 0; i getIntersectingRelations(Hashtable findConstant(Object[][] array, Context contex } /** - * @param array - * the array that contains pairs of paths and node sets + * @param array the array that contains pairs of paths and node sets * @return the node set of base nodes that we can start following those paths in * the array from, in order to reach at least one node at each node set * in all entries of the array @@ -1896,8 +1946,7 @@ public static LinkedList findBase(Object[][] array, Context context) { } /** - * @param array - * the array that contains pairs of paths and node sets + * @param array the array that contains pairs of paths and node sets * @return the node set of variable nodes that we can start following those * paths in the array from, in order to reach at least one node at each * node set in all entries of the array @@ -1915,8 +1964,7 @@ public static LinkedList findVariable(Object[][] array, Context contex } /** - * @param array - * the array that contains pairs of paths and node sets + * @param array the array that contains pairs of paths and node sets * @return the node set of pattern nodes that we can start following those paths * in the array from, in order to reach at least one node at each node * set in all entries of the array @@ -1933,7 +1981,7 @@ public static LinkedList findPattern(Object[][] array, Context context return result; } - private static VariableSet getAllVariables(Molecular node) { + public static VariableSet getAllVariables(Molecular node) { VariableSet result = new VariableSet(); Enumeration dCables = node.getDownCableSet().getDownCables().elements(); @@ -1954,45 +2002,73 @@ private static VariableSet getAllVariables(Molecular node) { return result; } + + + /* + public static Semantic SetType(Node b) { + + if (!(b.getSyntacticType().equals("Base"))) { + return null; + } + + else { + + Set relation = b.getUpCableSet().getUpCables().keySet(); + String [] relations = (String[]) relation.toArray(); + Semantic type = b.getSemantic(); + String sem = type.getSemanticType(); + for (int i = 0; i temp = (ArrayList) ns.readObject(); Network.savedNetworks = temp; ns.close(); } - + public static ArrayList getSavedNetworks() { return savedNetworks; } public static boolean addToSavedNetworks(String n) { boolean r; - if(savedNetworks.contains(n)) { + if (savedNetworks.contains(n)) { r = false; - }else { + } else { savedNetworks.add(n); r = true; } return r; } - + public static void deleteFromSavedNetworks(String f) { savedNetworks.remove(f); try { @@ -2056,75 +2132,76 @@ public static void deleteFromSavedNetworks(String f) { } } - public static void load(String relationsData, String caseFramesData, String nodesData, String molData, String mcd, String pcd, String vcd, String pNData, String nodesIndexData, String userDefinedMolSuffixData, String userDefinedPatSuffixData, String userDefinedVarSuffixData) throws IOException, ClassNotFoundException { - ObjectInputStream ris= new ObjectInputStream(new FileInputStream(new File(relationsData))); + public static void load(String relationsData, String caseFramesData, String nodesData, String molData, String mcd, + String pcd, String vcd, String pNData, String nodesIndexData, String userDefinedMolSuffixData, + String userDefinedPatSuffixData, String userDefinedVarSuffixData) + throws IOException, ClassNotFoundException { + ObjectInputStream ris = new ObjectInputStream(new FileInputStream(new File(relationsData))); Hashtable tempRelations = (Hashtable) ris.readObject(); Network.relations = tempRelations; ris.close(); tempRelations = null; - - ObjectInputStream cFis= new ObjectInputStream(new FileInputStream(new File(caseFramesData))); + + ObjectInputStream cFis = new ObjectInputStream(new FileInputStream(new File(caseFramesData))); Hashtable tempcF = (Hashtable) cFis.readObject(); Network.caseFrames = tempcF; cFis.close(); tempcF = null; - - - ObjectInputStream nodesis= new ObjectInputStream(new FileInputStream(new File(nodesData))); + + ObjectInputStream nodesis = new ObjectInputStream(new FileInputStream(new File(nodesData))); Hashtable tempNodes = (Hashtable) nodesis.readObject(); Network.nodes = tempNodes; nodesis.close(); tempNodes = null; - - ObjectInputStream molNodesis= new ObjectInputStream(new FileInputStream(new File(molData))); + ObjectInputStream molNodesis = new ObjectInputStream(new FileInputStream(new File(molData))); Hashtable tempMolNodes = (Hashtable) molNodesis.readObject(); Network.molecularNodes = tempMolNodes; molNodesis.close(); tempMolNodes = null; - - ObjectInputStream mc= new ObjectInputStream(new FileInputStream(new File(mcd))); + + ObjectInputStream mc = new ObjectInputStream(new FileInputStream(new File(mcd))); int tempMC = (int) mc.readObject(); Network.molCounter = tempMC; mc.close(); - - ObjectInputStream pc= new ObjectInputStream(new FileInputStream(new File(pcd))); + + ObjectInputStream pc = new ObjectInputStream(new FileInputStream(new File(pcd))); int tempPC = (int) pc.readObject(); Network.patternCounter = tempPC; pc.close(); - - ObjectInputStream vc= new ObjectInputStream(new FileInputStream(new File(vcd))); + + ObjectInputStream vc = new ObjectInputStream(new FileInputStream(new File(vcd))); int tempVC = (int) vc.readObject(); Network.varCounter = tempVC; vc.close(); - ObjectInputStream pn= new ObjectInputStream(new FileInputStream(new File(pNData))); + ObjectInputStream pn = new ObjectInputStream(new FileInputStream(new File(pNData))); Hashtable temppn = (Hashtable) pn.readObject(); Network.propositionNodes = temppn; pn.close(); - ObjectInputStream niis= new ObjectInputStream(new FileInputStream(new File(nodesIndexData))); + ObjectInputStream niis = new ObjectInputStream(new FileInputStream(new File(nodesIndexData))); ArrayList tempni = (ArrayList) niis.readObject(); Network.nodesIndex = tempni; niis.close(); - ObjectInputStream udmsis= new ObjectInputStream(new FileInputStream(new File(userDefinedMolSuffixData))); + ObjectInputStream udmsis = new ObjectInputStream(new FileInputStream(new File(userDefinedMolSuffixData))); LinkedList tempudms = (LinkedList) udmsis.readObject(); Network.userDefinedMolSuffix = tempudms; udmsis.close(); - ObjectInputStream udpsis= new ObjectInputStream(new FileInputStream(new File(userDefinedPatSuffixData))); + ObjectInputStream udpsis = new ObjectInputStream(new FileInputStream(new File(userDefinedPatSuffixData))); LinkedList tempudps = (LinkedList) udpsis.readObject(); Network.userDefinedPatSuffix = tempudps; udpsis.close(); - ObjectInputStream udvsis= new ObjectInputStream(new FileInputStream(new File(userDefinedVarSuffixData))); + ObjectInputStream udvsis = new ObjectInputStream(new FileInputStream(new File(userDefinedVarSuffixData))); LinkedList tempudvs = (LinkedList) udvsis.readObject(); Network.userDefinedVarSuffix = tempudvs; udvsis.close(); - + Node.setCount(nodes.size()); - + } /** From 161fa781df895370029fe622916c7e021db7bbdf Mon Sep 17 00:00:00 2001 From: Omarnael <47508294+Omarnael@users.noreply.github.com> Date: Wed, 15 Jul 2020 17:08:05 +0200 Subject: [PATCH 3/4] Modifications in Semantics Modifications in Semantic & Semantic hierarchy --- src/sneps/network/classes/Semantic.java | 31 +++- .../network/classes/SemanticHierarchy.java | 137 +++++++++++++++++- 2 files changed, 156 insertions(+), 12 deletions(-) diff --git a/src/sneps/network/classes/Semantic.java b/src/sneps/network/classes/Semantic.java index f5e08058..eb7c762e 100644 --- a/src/sneps/network/classes/Semantic.java +++ b/src/sneps/network/classes/Semantic.java @@ -1,14 +1,18 @@ package sneps.network.classes; import java.io.Serializable; +import java.util.ArrayList; import java.util.LinkedList; import sneps.exceptions.SemanticNotFoundInNetworkException; public class Semantic implements Serializable { protected String semanticType; - private String superClass; - public static Semantic act, proposition, infimum, individual; + public String superClass; + public ArrayList superClassSet; + public static Semantic act, proposition, entity, individual; + public static Semantic infimum; + public Semantic() { } @@ -22,6 +26,11 @@ public Semantic(String sem, String superClass) { semanticType = sem; this.superClass = superClass; } + + public Semantic(String sem, ArrayList Semantics) { + semanticType = sem; + this.superClassSet = Semantics; + } public LinkedList getSuperClassesNames() { // this list will get populated with all the names of all the parent classes to this class @@ -51,17 +60,25 @@ private void getSuperClassesNamesRecursively(LinkedList superClassesName public String getSemanticType() { return semanticType; } + + public ArrayList getSuperClassSet() { + return superClassSet; + } public boolean isAsserted(Object contextByName) { // TODO Auto-generated method stub return false; } - public static void createDefaultSemantics() { - act = SemanticHierarchy.createSemanticType("Act"); - proposition = SemanticHierarchy.createSemanticType("Proposition"); - infimum = SemanticHierarchy.createSemanticType("Infimum"); - individual = SemanticHierarchy.createSemanticType("Individual"); + public static void createDefaultSemantics() throws SemanticNotFoundInNetworkException { + + entity = SemanticHierarchy.createSemanticType("Entity"); + infimum = SemanticHierarchy.createSemanticType("Infimum"); // FOR TESTING ONLY + act = SemanticHierarchy.createSemanticType("Act","Entity"); + proposition = SemanticHierarchy.createSemanticType("Proposition","Entity"); + individual = SemanticHierarchy.createSemanticType("Individual","Entity"); + } + } diff --git a/src/sneps/network/classes/SemanticHierarchy.java b/src/sneps/network/classes/SemanticHierarchy.java index dd246813..c5dbdeb5 100644 --- a/src/sneps/network/classes/SemanticHierarchy.java +++ b/src/sneps/network/classes/SemanticHierarchy.java @@ -8,23 +8,35 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashSet; import java.util.Hashtable; import java.util.LinkedList; +import java.util.Set; import sneps.exceptions.SemanticNotFoundInNetworkException; import sneps.network.Network; public class SemanticHierarchy implements Serializable{ - private static Hashtable semantics = new Hashtable(); + public static Hashtable semantics = new Hashtable(); public static Semantic createSemanticType(String identifier) { if (semantics.containsKey(identifier)) { return semantics.get(identifier); } else { - Semantic semantic = new Semantic(identifier); - semantics.put(identifier, semantic); - return semantics.get(identifier); + + if((identifier.equals("Entity"))) { + Semantic semantic = new Semantic(identifier); + semantics.put(identifier, semantic); + return semantics.get(identifier); + } + + else { + Semantic semantic = new Semantic(identifier,"Entity"); + semantics.put(identifier, semantic); + return semantics.get(identifier); + } } } @@ -43,6 +55,121 @@ public static Semantic createSemanticType(String identifier, String superClassId } } } + + public static Semantic createSemanticType(String identifier, ArrayList superSemanticSet) { + + if(semantics.containsKey(identifier)) { + return semantics.get(identifier); + } + + else { + + boolean flag = true; + + for (int i = 0; i findChildren(Semantic sem) throws SemanticNotFoundInNetworkException { + Hashtable semantics1 = SemanticHierarchy.getSemantics(); + Set semantics2 = semantics1.keySet(); + Object[] semantics = semantics2.toArray(); + + ArrayList children = new ArrayList(); + + for (int i = 0; i semantics) { + HashSet semanticSet = new HashSet(); + for (Semantic s : semantics) { + if (semanticSet.add(s) == false) { + return s; + } + + } + return null; + } + + + + public static Semantic greatestLowerBound(Semantic sem1, Semantic sem2) throws SemanticNotFoundInNetworkException { + ArrayList subtypes = new ArrayList(); + subtypes.addAll(findChildren(sem1)); + subtypes.addAll(findChildren(sem2)); + Semantic glb = new Semantic(); + + while(!(subtypes.isEmpty())) { + + for (int i = 0; i Date: Wed, 15 Jul 2020 17:22:52 +0200 Subject: [PATCH 4/4] Update NodeSet.java Minor issues with set operations --- src/sneps/network/classes/setClasses/NodeSet.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sneps/network/classes/setClasses/NodeSet.java b/src/sneps/network/classes/setClasses/NodeSet.java index 4634eae3..dc83258d 100644 --- a/src/sneps/network/classes/setClasses/NodeSet.java +++ b/src/sneps/network/classes/setClasses/NodeSet.java @@ -56,19 +56,19 @@ public NodeSet Union(NodeSet ns) { return unionSet; } - public NodeSet Intersection(NodeSet ns) { + public NodeSet difference(NodeSet ns) { NodeSet intersectionSet = new NodeSet(); for (int i = 0; i < ns.size(); i++) { - if (this.contains(ns.getNode(i))) + if (!this.contains(ns.getNode(i))) intersectionSet.addNode(ns.getNode(i)); } return intersectionSet; } - public NodeSet difference(NodeSet ns) { + public NodeSet Intersection(NodeSet ns) { NodeSet differenceSet = new NodeSet(); for (int i = 0; i < this.size(); i++) { - if (!ns.contains(this.getNode(i))) + if (ns.contains(this.getNode(i))) differenceSet.addNode(this.getNode(i)); } return differenceSet;